Directives with isolated scope + one-way data-binding
Hi all,
I wanted to discuss about a particular behavior in directives with isolated scope. With One-way data binding arguments, objects are not passed like in two-way data binding.
Examples : http://plnkr.co/edit/bTpYV7dRMrHO0fkSGn1i?p=preview
There is few documentation about that. Is it a normal behavior ? If yes, is it possible to have more information about that?
Thanks
This is explained at http://docs.angularjs.org/api/ng/service/$compile where the definition of a directive scope is defined. There it is explained that for one way data binding, the resulting type is always a string
Oh thanks.
Why is this the case? What if we're passing a large data object to a directive that contains nothing needing to be watched? Isn't this a performance issue?
@marketingpartnersau can you explain what you mean?
There isn't really a "one-way binding" vs "two-way binding", what there is is "@" for interpolation (which means, if we see that there are interpolation start/end markers, we watch the interpolation expression --- otherwise we just treat it as a regular string. interpolation always results in a string value) vs value binding (which is sometimes called two-way binding because the isolate scope can modify the parent scope and vice versa) which is really just supposed to be a (maybe assignable) expression evaluated on the parent scope, the value gotten from evaluating the expression is assigned to the child scope --- if the child scopes value changes to something not in the parent scope, the parent scope tries to be updated (throws if not able to assign) with the value from the child scope. This is always by value, and doesn't convert to strings.
None of these ever care about t"deep watching", and therefore the size of the object used in "=" bindings really is of no consequence --- it gets watched by identity regardless. "@" may or may not be watched, depending on the presence of interpolation markers. This can be expensive, but it's not really a massively expensive operation, just don't go too crazy with it.
Anyways yeah, the depth or number of properties in an object used in "=" bindings is really irrelevant --- I'm not sure if that answers the question, because it's not really clear what's being asked, but "=" binding watches are always shallow
OK, thanks Caitlin. That answers my question - was just concerned that the object gets watched deeply when in this case it is 100% not necessary as I know none of the properties will change, even down to the deepest level. (I think 5 or 6). Just didn't want to overload memory with unnecessary tasks :)
I have a related question. Recent Angular versions introduce the '=*' binding, which set the watchers using $watchCollection instead of $watch. Now the thing is... When am I supposed to use that binding instead of plain old '='? I mean, if you are changing a property/adding an item/whatever there is no need to update the reference the other scope has, unless I'm missing something.