Ben Aston
Ben Aston

Reputation: 55729

Directive definition syntax clarification

If I declare the following isolate scope in a directive definition:

scope: {
    state: '=', 
},

Is it accurate to say the = links the state property on the isolate scope to the value of returned by the expression associated with an attribute named state on the directive declaration, run in the parent scope?

Upvotes: 0

Views: 31

Answers (1)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

For example say your directive is like below,

<div ng-controller="testCtrl">
    <directive-name state="scope_variable"></directive-name>
</div>

then if we use like this

scope: {
    state: '=', 
},

state attribute on the directive should be a $scope (testCtrl's) variable, If its a scope variable then state is pointing to same variable in the testCtrl's scope.

or a expression for ex like ...state="'scope_variable'"... then it's a String and it's not pointing to a scope variable, and also if you defined it as ...state="1+2"... then its a expression so that state variable inside directive scope is equals to 3.

hope this make sense.

here is a demo.

note

if we define the directive as isolated scope scope: {} then angular will create a child scope from its immediate parent, (parent scope of the previous example is scope of testCtrl).

see the console of the provided demo and see the console.log(scope.$parent.name); is pointing to the $scope.name of MainCtrl's scope.

Upvotes: 1

Related Questions