Welcome everyone to guide and discuss:)
Objective
Angular will expand this standard browser process to create a angular context. This angular context refers to the specific code that runs within the angular event loop, which is called a $digest loop. The $digest loop has two components, the $watch list and the $evalasync list, respectively.
For example, when the <input> tag binds the $scope.name property, angular needs to track the change in order to update the view. It does this by adding a monitoring function to the $watch list. The $watch list is parsed by a program called "Dirty Value Checking" in the $digest loop.
Dirty value Check
the angular will continuously track the values of the current monitoring. Angular will traverse the $watch list, and it will continue to traverse the watch list if the value updated from the old value does not change. If the value has changed, angular will enable the new value and continue traversing the $watch list. Angular will traverse the entire list, and as long as any value changes, it will be returned to the $watch loop until it detects that no change has occurred. If the loop runs 10 or more times, angular throws an exception and stops running.
Example
A school, students are to stay here, but there are several students are not good, all day bad to sleep, they know to play games. The school leader knows and asks the teacher to say: "hey! Dude, the students in these dorms are especially naughty, you help me look at them a lot! "and gave the teacher a list of the dorms (the monitor function was added to the $watch lists). Every night, the teacher will constantly patrol the dormitory list of the several quarters (angular traversal monitoring list). Suddenly one day, the teacher in the inspection of 432 quarters when found Xiao Ming incredibly bad to sleep but playing games! (Angular found a change), the teacher taught Xiao Ming a meal, and then let it continue to sleep (enable the new value after the change). But the teacher thought, just now Xiao Ming is not in the other dorm with the students to play online games it? I have to go on patrol. (Affected by the value of the change, $watch other values in the list, may or may not be followed by a change.) $scope.num1=1, for example, has now changed, $scope. num1=10. Then, $scope.num2=2, will not be affected by $SCOPE.NUM1, but $scope.num3=num1+10, its value from the original 11 into 20, so angular will conduct two inspections, until the value of the listener has not changed again.
$watch
The $watch function returns a logout function to the listener function, and we can cancel angular listening to the current value by calling the logout function.
var unregisterwatch = $scope. Watch (' Model.email ', function(newval, Oldval) { if return // Initialize if (newval! = oldval) {// } } )
If you are listening to the completion of this value, you can cancel the monitoring by calling Unregisterwatch () . Where the listener function is called once at initialization time, newval = = = Oldval are undefined
$watchCollection
You can use this function to set light monitoring for an object's properties or array elements
$scope. Watchcollection (' names ', function(newnames, oldnames) { // } )
$apply () and $digest ()
The $apply () function allows the expression to be executed inside the angular context from outside the angular framework and allows the result to be controlled (Digest loop). For example, if you implement a settimeout () and want the event to run inside the angular context, you must use $apply ()
$scope. Apply (function() { setTimeout () {//... },)})
or digest loops directly by calling the $digest () function to check for dirty values
// .. Some action$scope. $digest ();
Resources
"Angularjs authoritative guide" P326
Digest cycle $apply in Angularjs