How does data binding work in the AngularJS
framework?
I haven\'t found technical details on their site. It\'s more or less clear how it works when data
Explaining with Pictures :
The reference in the scope is not exactly the reference in the template. When you data-bind two objects, you need a third one that listen to the first and modify the other.
Here, when you modify the , you touch the data-ref3. And the classic data-bind mecanism will change data-ref4. So how the other
{{data}}
expressions will move ?
Angular maintains a oldValue
and newValue
of every binding. And after every Angular event, the famous $digest()
loop will check the WatchList to see if something changed. These Angular events are ng-click
, ng-change
, $http
completed ... The $digest()
will loop as long as any oldValue
differs from the newValue
.
In the previous picture, it will notice that data-ref1 and data-ref2 has changed.
It's a little like the Egg and Chicken. You never know who starts, but hopefully it works most of the time as expected.
The other point is that you can understand easily the impact deep of a simple binding on the memory and the CPU. Hopefully Desktops are fat enough to handle this. Mobile phones are not that strong.