简体   繁体   中英

Angular JS digest cycle invoked multiple times

I am trying to understand the Angular1 $digest cycle. I've read in multiple articles that the digest cycle is called multiple times on every change.

In this article it says:

So once we traversed all of the $watch functions in the $watch list, are we done with the $digest loop? No, we go through the list ONE more time and verify that nothing has changed. We do this because there could have been a change to one of the values when another $watch item updated it. We continue through this loop until no changes are present in any of the values.

So my question is - in what scenario there could have been a change to one of the values when another $watch item updated it?

this may not be the perfect example in real use cases, but things like this could happen.

$scope.$watch('Var_A', function(){
    // change Var_B
});

$scope.$watch('Var_B', function(){
   // do something
});

Var_A and Var_B are put into the $watch list. Say that there is an angular change that changes Var_A, it triggers the first $digest cycle and Var_B changes. The first $digest cycle is not aware Var_B has changed. It then goes through the list one more time and will find that Var_B changes. After this, it goes through the list again and makes sure nothing in the $watch list got changed.

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