preface
It’s been a while since we used the Angular1. x version for work reasons, and although angualR2 has been fully refactured since the update, each version exists for a certain reason. Without further ado, let’s get to the point.
1. What is the principle of bidirectional binding?
AngualrJS’s bidirectional binding is based on dirty checking. There is a listening queue on $scope. Whenever a value is bound to the view, a watch is inserted into the Watch list. Iterate through all the watches and update the DOM.
2. What action triggers the $digest loop?
UI changes, Ajax requests, ng-click,$timeout…
3. If a Watch contains another Watch, the corresponding scope Model of the watch changes, how to deal with this situation?
The $Digest loop does not run only once; once Angualar runs the entire $watch list, if any values change, the application will fall back to the $Watch loop until it detects that nothing has changed.
4. As mentioned above, if any value changes, it will fall back to the Watch loop. Will this cause an infinite loop?
No. If the loop runs more than 10 times, Angular throws an exception and the application dies.
5. The following code, ng-click does nothing, will there be a dirty check?
<button ng-click="">click</button>
Copy the code
Will be.
6. Will the expression bound to the hidden element be checked?
Will be.
7. Are repeated expressions double-checked?
Will be. In summary, a bound expression is monitored whenever it is in the current DOM tree, whether it is visible or not, whether it is placed in another Tab, and whether it is associated with user actions.
8. The difference between $apply and $digest
$apply calls $digest from rootScope
9. When is $apply invoked manually?
I can think of two scenarios
- Custom directives that bind data to $scope and update the DOM
- The setTimeout callback involves manipulating the data on $scope to update the corresponding view. $scope.$apply = $scope.$apply = $scope.
Afterword.
It is the first time to write on the nuggets, if there is an incorrect place to write, welcome to leave a message to discuss. An accompanying article is the-digest-loop-and-apply