Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Documentation on Isolate Scope Attributes

Is there any AngularJS documentation providing a straightforward definitive list of ways to handle attributes in a directive with isolate scope?

The directive guide touches upon the use of = but doesn't list the other options used for binding.

So far, I'm aware of (via mixed sources):

scope: {
   myAttr1: '=attr1',
   myAttr2: '=?attr2',
   myAttr3: '@attr3',
   myAttr4: '&attr4'
},
like image 576
Philip Bulley Avatar asked Jan 23 '14 10:01

Philip Bulley


2 Answers

have a look at the compile service: http://docs.angularjs.org/api/ng.$compile

The 'isolate' scope takes an object hash which defines a set of local scope properties derived from the parent scope. These local properties are useful for aliasing values for templates. Locals definition is a hash of local scope property to its source:

@ or @attr - bind a local scope property to the value of DOM attribute. The result is always a string since DOM attributes are strings. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localName:'@myAttr' }, then widget scope property localName will reflect the interpolated value of hello {{name}}. As the name attribute changes so will the localName property on the widget scope. The name is read from the parent scope (not component scope).

= or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localModel:'=myAttr' }, then widget scope property localModel will reflect the value of parentModel on the parent scope. Any changes to parentModel will be reflected in localModel and any changes in localModel will reflect in parentModel. If the parent scope property doesn't exist, it will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception. You can avoid this behavior using =? or =?attr in order to flag the property as optional.

& or &attr - provides a way to execute an expression in the context of the parent scope. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localFn:'&myAttr' }, then isolate scope property localFn will point to a function wrapper for the count = count + value expression. Often it's desirable to pass data from the isolated scope via an expression and to the parent scope, this can be done by passing a map of local variable names and values into the expression wrapper fn. For example, if the expression is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}).

like image 59
michael Avatar answered Oct 23 '22 07:10

michael


You can also look at the older documentation of directive. I believe the new one is better to understand but the older one has more meat.

http://code.angularjs.org/1.1.5/docs/guide/directive

like image 28
Chandermani Avatar answered Oct 23 '22 06:10

Chandermani