Angular $ scope. $ digest vs $ scope. $ apply

I just want to know how to use $digest . Inside the controller, the following code works fine, and it updates the DOM after 3 seconds:

 setTimeout(function(){ $scope.$apply(function(){ $scope.name = 'Alice'; }); },3000); 

However, using

 setTimeout(function(){ $scope.$digest(function(){ $scope.name = 'Alice'; }); },3000); 

Nothing happens...

I thought they were doing the same thing. What am I wrong about?

+8
source share
4 answers

$apply() and $digest() have some similarities and differences. They are similar in that they both verify that the user interface has changed and updated, and fired all observers.

One of the differences between them is what they are called. $digest() is called without any argument. $apply() performs the function that is performed before any updates are performed.

Another difference is that they influence. $digest() update the current scope and all child scopes. $apply() update each area. Therefore, most of the time $digest() will be what you want and more efficient.

The final difference explaining why $apply() accepts a function is how they handle exceptions in observers. $apply() will throw exceptions in $exceptionHandler (uses an internal try-catch block), and $digest() will require you to handle the exceptions yourself.

+40
source

I think you should go through the $ apply docs

$ apply () is used to execute an expression in angular from outside the angular framework

Usually you do not call $ digest () directly in controllers or directives. Instead, you should call $ apply () (usually from within the directive), which will force $ digest ().

Just like a suggestion from Jorg, use $ timeout

+2
source

In angularjs, the $scope object has various functions, such as $watch() , $digest() and $apply() and we will call these functions central functions.

The central functions angularjs $watch() , $digest() and $ apply are used to bind data to visible variables and to monitor changes in the variables.

Usually in angularjs we use the $ scope object to bind data to variables and use the values ​​of these variables wherever we need in the application. In angularjs, any variables that we assign to the $ scope object will be added to the watchlist using the $scope.$watch() function.

In angularjs, when variables are added to the $scope.digest() list, the $scope.digest() function will $scope.digest() over the watchlist variables and check if any changes have been made to these variables or not. In case any changes are found for the variables of this watchlist, immediately the corresponding function of the event listener will call and update the corresponding values ​​of the variables with the new value taking into account the application.

The $scope.$apply() Apply $scope.$apply() function Apply $scope.$apply() Apply $scope.$apply() in angularjs is used whenever we want to integrate any other code with angularjs. Typically, the $scope.$apply() function executes user code and then forcibly calls the $scope.$digest() function to check all the watchlist variables and update the variable values ​​if any changes to the watchlist variables are detected.

In most cases, angularjs will use the $scope.watch() and $scope.digest() functions to check and update values ​​based on changes in variable values.

+2
source

This article gives a better understanding of $ watch, $ apply and $ digest .

Just,
$ watch, which is used to detect changes in the user interface
$ apply tells the $ digest cycle what changes to apply
The $ digest loop will start and each $ watch will request changes, the DOM will change according to what was applied

You should only call $ apply .

0
source

Source: https://habr.com/ru/post/1244515/


All Articles