0

There is a behaviour with isolated scope in directives and = attribute I don't understand.

I have a directive with an isolated scope and two attributes in it :

scope: {
    attr1: '=',
    attr2: '='
} 

Maybe I am wrong but i'm expecting that modifications of the attr1 and attr2 objects in the directive's template will be transmitted to the original object.

for exemple :

<directivename attr1="anObject" attr2="anotherObject" />

I'm expecting that modifications on attr1 and attr2 in the directive are also available in 'anObject' and 'anotherObject' but this doesn't seem to be the case.

But I have notice that if I add a level to anObject and anotherObject

<directivename attr1="foo.anObject" attr2="foo.anotherObject" />

then any modifications in the directive's template of attr1 and attr2 are transmitted to $scope.foo.anObject and $scope.foo.anotherObject.

Can somebody explain me why it is like this and tell me what is the good way to force the transmission of modifications from the directive to the original objects without having to force the caller of the directive to add a intermediate level between $scope and the object.

Thank you :)

user3060886
  • 777
  • 1
  • 6
  • 10

1 Answers1

0

I'm going to guess that you are using a primitive for your attribute value rather than an object. If so, what you are seeing is the overwriting of the primitive by child scope. See this post for details (especially the 2nd answer):

What is the angularjs way to databind many inputs?

Community
  • 1
  • 1
user1821052
  • 484
  • 5
  • 14