Testtest1322015-03-15 23:11:31
Testtest132, 2015-03-15 23:11:31

Angular $digest loop?

For example, we added the expression {{ hello }} and Angular created a function for it that will track changes. Then we added the ng-click directive and hung the handler. It is clear that Angular launches a $digest cycle, in fact, thanks to which the 2-way data binding works. BUT, I'll quote:

Angular is not that easy. AngularJS doesn't call $digest directly, it calls the $scope.$apply() function which calls $rootScope.digest(). And as a result, the digest cycle in $rootScope visits the child $scopes one by one and calls their digest cycles.

Why is digest launched on the rootScope, if there are a lot of watchers there, then the application will slow down (or it means that it does not touch the parent scope)?
if the above is true, then what about the parent scope, if, for example, there is a model in $rootScope and it can be changed in any controller?
and what if there are many children - what will be the depth?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Mikhail Osher, 2015-03-15

The maximum depth will be
You never know, you have a service that is used in 10 controllers within the same page in different views. How can they find out about the changes? - $rootScope.$digest() .
When I am sure that I need to run something within one element, and this is VERY performance critical, I will create a custom directive in which instead of $scope.$apply() I will write $scope.$$phase || $scope.$digest() . Only watchers within the same $scope will be checked .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question