Angular: why isn't $ evalAsync called $ applyAsync?

Angular novice area question ( docs here ).

  • $eval executes the expression in the context of the scope.
  • $apply basically calls $eval and then $digest .
  • Why $evalAsync call $digest too (or, more accurately, provide a call to $digest )?

It seems that $ eval Async really should be called $ apply Async , right?

I'm new - what am I missing?

+4
angularjs asynchronous definition angular-digest
source share
2 answers

$evalAsync and $applyAsync for different situations.

$ evalAsync : will cancel the expression for the next iteration loop of the current digest loop . One cycle of processing cycles Angular cycle several times until the data is dirty. If the digest cycle is not executed, it will start a new digest cycle (call $apply ) and evaluate the expression (call $eval method) in it. This method is useful if you are calling a function from an Angular scope, but still want to digest the dirty data when the digest cycle is already running, in which case you cannot call $apply .

$ applyAsync : cancel the expression before the next digest cycle . It always starts a new digest cycle after evaluating the expression (calling the $apply method). This method is useful if you often make some sort of utility call (e.g. $http service) outside the Angular area with dirty area data. However, if it starts a digest for each callback, there may be poor performance. Therefore, this method optimizes the process by separating digest cycles among several asynchronous callbacks, which is superior to the $evalAsync method.

+2
source share

$ applyAsync already exists:

Schedule a call to $ apply to happen later. Actual time differences vary across browsers, but usually around ~ 10 milliseconds.

This can be used to queue multiple expressions that need to be evaluated in the same digest.

evalAsync handles digests in different ways:

$ evalAsync starts the digest and is unusable when it is expected that the digest should not occur.

Note: if this function is called outside the $ digest loop, a new $ digest loop will be scheduled.

This behavior, when called implicitly modified in AngularJS 1.3:

-Failed, even if invokeApply was set to false, the $ rootScope digest will occur during the promise. This is no longer the case, since promises returns from $ timeout, and $ interval no longer triggers $ evalAsync (which in turn causes $ digest) if invokeApply is false.

Workarounds include manually starting $ scope. $ apply () or return $ q.defer (). Promise from the promise callback and allow or reject it when necessary.

References

+1
source share

All Articles