This question has been already beaten to death, but I'll share this anyway in case someone else out there is struggling with the horrible mess that is AngularJS scopes. This will cover =
, <
, @
, &
and ::
. The full write up can be found here.
=
establishes a two way binding. Changing the property in the parent will result in change in the child, and vice versa.
<
establishes a one way binding, parent to child. Changing the property in the parent will result in change in the child, but changing the child property will not affect the parent property.
@
will assign to the child property the string value of the tag attribute. If the attribute contains an expression, the child property updates whenever the expression evaluates to a different string. For example:
<child-component description="The movie title is {{$ctrl.movie.title}}" />
bindings: {
description: '@',
}
Here, the description
property in the child scope will be the current value of the expression "The movie title is {{$ctrl.movie.title}}"
, where movie
is an object in the parent scope.
&
is a bit tricky, and in fact there seems to be no compelling reason to ever use it. It allows you to evaluate an expression in the parent scope, substituting parameters with variables from the child scope. An example (plunk):
<child-component
foo = "myVar + $ctrl.parentVar + myOtherVar"
</child-component>
angular.module('heroApp').component('childComponent', {
template: "<div>{{ $ctrl.parentFoo({myVar:5, myOtherVar:'xyz'}) }}</div>",
bindings: {
parentFoo: '&foo'
}
});
Given parentVar=10
, the expression parentFoo({myVar:5, myOtherVar:'xyz'})
will evaluate to 5 + 10 + 'xyz'
and the component will render as:
<div>15xyz</div>
When would you ever want to use this convoluted functionality? &
is often used by people to pass to the child scope a callback function in the parent scope. In reality, however, the same effect can be achieved by using '<' to pass the function, which is more straightforward and avoids the awkward curly braces syntax to pass parameters ({myVar:5, myOtherVar:'xyz'}
). Consider:
Callback using &
:
<child-component parent-foo="$ctrl.foo(bar)"/>
angular.module('heroApp').component('childComponent', {
template: '<button ng-click="$ctrl.parentFoo({bar:'xyz'})">Call foo in parent</button>',
bindings: {
parentFoo: '&'
}
});
Callback using <
:
<child-component parent-foo="$ctrl.foo"/>
angular.module('heroApp').component('childComponent', {
template: '<button ng-click="$ctrl.parentFoo('xyz')">Call foo in parent</button>',
bindings: {
parentFoo: '<'
}
});
Note that objects (and arrays) are passed by reference to the child scope, not copied. What this means is that even if it's a one-way binding, you are working with the same object in both the parent and the child scope.
To see the different prefixes in action, open this plunk.
::
[Official docs]
Later versions of AngularJS introduce the option to have a one-time binding, where the child scope property is updated only once. This improves performance by eliminating the need to watch the parent property. The syntax is different from above; to declare a one-time binding, you add ::
in front of the expression in the component tag:
<child-component
tagline = "::$ctrl.tagline">
</child-component>
This will propagate the value of tagline
to the child scope without establishing a one-way or two-way binding. Note: if tagline
is initially undefined
in the parent scope, angular will watch it until it changes and then make a one-time update of the corresponding property in the child scope.
Summary
The table below shows how the prefixes work depending on whether the property is an object, array, string, etc.
=
is used in directive isolate scope to enable two way binding and@
does not updates model, only updates Directive scope values. – STEEL