简体   繁体   中英

Angularjs 1.3+ One- vs Double Way Data Binding

I'm new to Angular and it is not clear where the double data binding is needed except from the classic ng-model scenario where the model value is reflected in the view.

Do I need double binding for ng-repeat variables for example? Or do I need it for variables that can be changed in $watch?

I read that one-way data binding can have significant performance improvements so I do not want to use double data binding unless I really need it

For example

 <img ng-src="{{mySrc}}"> or
 <img ng-src="{{::mySrc}}">

 <div ng-repeat="item in items"> or
<div ng-repeat="::item in ::items"> where items retrieved from $http

2-way data binding refers directly to the ng-model directive, it's meaning is that you can change a model value in the view (via an input ng-model ) and also change it programmaticly, either change will be replicated to the other and in the view.

ng-repeat is simply an expression that gets evaluated when a digest cycle starts so this is not 2-way data binding.

In terms of performance angulars binding process uses dirty checking and is not ideal, using an ng-model or not will make no difference to this. The digest cycle will only start if the model value changes or if you change a model value in angulars context.


After seeing the code snippet you provided the {{::model}} notation will simply create a one time binding, this means that the value will never change in your view and it won't be watched.

The different is {{value}} gets watched, when it changes this binding gets updated, this is slow on performance due to dirty checking however in your example the value is not watched, it's never checked and if used in a conditional statement is only evaluated once.

It's up to you if you want to use either, if a value will never change then use {{::}} as it's fast, if the values going to change in the future use {{}}

Using double binding is useful only if your model is subjected to constant changes. In case your model is going to be a constant and doesn't need frequent changes than single binding would be effective. If you use too many double bindings even where it is not necessary it could lead to serious overhead in AngularJS and can bog down your site. That is because each double binding is monitored via watch by angular.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM