0

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?

1
  • 1
    The documentation puts it this way: "localModel (=state) will reflect the value of parentModel (=state) on the parent scope". Parentheses added by me. Commented Oct 1, 2015 at 9:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, that makes, sense, but can you confirm that my description in the question is correct? Specifically that the expression is evaluated in the parent scope.
i update the answer, please check it. :), what they refering to parent scope is the scope of testCtrl in the example.
So, if my understanding of your answer is correct, then my statement in the question was correct?
I have added a simple demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.