I struggled a bit with this documentation too when first getting into angular, but I will make an attempt try to clarify things for you. First, when using this scope
property, it creates an “isolated scope.” All this means is that it won’t inherit any properties from parent scopes, and so you don’t have to worry about any collisions within the scope.
Now, the ‘@’ notation means that the evaluated value in the attribute will automatically get bound into your scope for the directive. So, <my-directive foo="bar" />
would end up with the scope having a property called foo
that holds the string “bar”. You could also do something like <my-directive foo="{{bar}}"
And then the evaluated value of {{bar}}
will be bound to the scope. Since attributes are always strings, you will always end up with a string for this property in the scope when using this notation.
The ‘=’ notation basically provides a mechanism for passing an object into your directive. It always pulls this from the parent scope of the directive, so this attribute will never have the {{}}
. So, if you have <my-directive foo="bar" />
it will bind whatever is in $scope.bar
into your directive in the foo
property of your directive’s scope. Any change’s you make to foo
within your scope will be refelected in bar
in the parent scope, and vice versa.
I haven’t used the ‘&’ notation nearly as much as the other too, so I don’t know it as well as those two. From what I understand, it allows you to evaluate expressions from the context of the parent scope. So if you have something like <my-directive foo="doStuff()" />
, whenever you call scope.foo() within your directive, it will call the doStuff function in the directive’s parent scope. I’m sure there’s a lot more you can do with this, but I’m not as familiar with it all. Maybe someone else can explain this one in more detail.
If just the symbol is set in the scope, it will use the same name as the attribute to bind to the directives scope. For example:
scope: {
foo1: '@',
foo2: '=',
foo3: '&'
}
When including the directive, there would need to be the attributes foo1, foo2, and foo3. If you want a property in your scope different than the attribute name, you can specify that after the symbol. So, the example above would be
scope: {
foo1: '@bar1',
foo2: '=bar2',
foo3: '&bar3'
}
When including the directive, there would need to be the attributes bar1, bar2, and bar3, and these would get bound in the scope under properties foo1, foo2, and foo3 respectively.
I hope this helps. Feel free to ask questions with which I can clarify my answer.