AngularJS
Web Development
JavaScript
Programming
Directive Scope

What is the difference between '@' and '=' in directive scope in AngularJS?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In AngularJS, directives are a core feature that allow you to create reusable components. A crucial aspect of directives is how they handle scope. Specifically, directives allow you to isolate a scope, and define how data is passed between this isolated scope and the parent scope using scope bindings. Among the most commonly used bindings are '@' and '='. Understanding the difference between these two can significantly impact how components interact and manage data in AngularJS applications.

@ Binding (Attribute Binding)

The '@' in a directive's scope declaration is used for one-way text binding between a directive's isolated scope and the parent scope. Essentially, it passes the value of a DOM attribute from the parent scope as a string to the directive. This type of binding is useful when you want to pass a static value or one-way expression from the outer scope.

How it works

When you use '@', AngularJS passes the value as a string, which means any changes made to this value within the directive do not reflect back to the parent scope. It is typically used for passing strings such as configuration values, labels, or any other data that is not expected to change by the directive.

Example:

html
<div ng-controller="MainCtrl">
  <my-directive my-attr="Hello, World!"></my-directive>
</div>
javascript
1angular.module('myApp', [])
2  .controller('MainCtrl', function($scope) {})
3  .directive('myDirective', function() {
4    return {
5      restrict: 'E',
6      scope: {
7        myAttr: '@'
8      },
9      template: '<p>{{ myAttr }}</p>'
10    };
11  });

In this case, myAttr within myDirective will have the string value "Hello, World!".

= Binding (Two-way Binding)

The '=' in a directive's scope declaration allows for two-way data binding between the directive's isolated scope and the parent scope. This is used when you want the directive to maintain an interactive relationship with the parent scope, reflecting changes in both directions.

How it works

When you use '=', AngularJS sets up a watcher on the specified attribute's value in both the parent and child scopes. As a result, if the value changes in the directive's scope, the parent scope will update accordingly and vice versa. This type of binding is suited for scenarios where the directive needs to manipulate shared data.

Example:

html
<div ng-controller="MainCtrl">
  <my-directive my-prop="data"></my-directive>
</div>
javascript
1angular.module('myApp', [])
2  .controller('MainCtrl', function($scope) {
3    $scope.data = "Initial Value";
4  })
5  .directive('myDirective', function() {
6    return {
7      restrict: 'E',
8      scope: {
9        myProp: '='
10      },
11      template: '<input type="text" ng-model="myProp" />'
12    };
13  });

In this example, any changes made to myProp through the input field in myDirective will be reflected in the MainCtrl's data property.

Comparison Table

Binding TypeData FlowUse Case
@One-way (Parent to Child)Passing static or configuration data as a string
=Two-waySharing dynamic data that directive can modify

Additional Considerations

  • Expression (& Binding): Apart from '@' and '=', AngularJS provides '&' binding, which allows function binding for passing callbacks to directives.
  • Impact on Performance: Using two-way binding ('=') excessively can lead to performance issues due to the number of watchers it creates in large applications.

Understanding the differences between '@' and '=' in AngularJS allows developers to make informed decisions about how to structure interactions between directives and their surrounding scopes, improving both performance and maintainability of applications.


Course illustration
Course illustration

All Rights Reserved.