What is the point of having unidirectional binding (<) in Angular 1.5?

With the introduction of the component concept, Angular 1.5 also introduced one-way data binding (<). However, the manual says:

Please note, however, that both the fields of the parent and the component refer to the same object, so if you change the properties of the object or the array elements in the component, the parent will still reflect this change. Therefore, the general rule should be to never change the property of an object or array in the component area.

Indeed, changing the property of an object in the directive / component area is reflected in the parent area - not like unidirectional binding, it just adds confusion to all this - now we have unidirectional binding, which is better not to use for objects or arrays, whereas for strings we have @.

What could be a useful scenario for using one-way data binding in the real case? Or is it a good idea to completely avoid this, in order to protect yourself from unexpected changes - since a unidirectional concept screams that it is unidirectional, but it is not?

+5
source share
1 answer

In the case of @ you should use interpolation (expression {{value}} ) to pass some value to the directive:

 <my-directive some-value="{{value}}"></my-directive> 

and

 scope { someValue: "@" } 

In the case of binding < you set the attribute value without interpolation. And when the value not undefined , it will be limited to an isolated area:

 <my-directive some-value="{{value}}"></my-directive> 

and

 scope { someValue: "<" } 

Of course, someValue should be primitive, not an array or object

0
source

Source: https://habr.com/ru/post/1242945/


All Articles