Angular beginner question about scopes (docs here).
$eval
executes an expression in the context of a scope.$apply
basically calls$eval
and then$digest
.- Why does
$evalAsync
call$digest
too (or, more precisely, ensure$digest
is called)?
It seems to be that $
eval
Async
should really be called $
apply
Async
, doesn't it?
I'm a beginner -- what am I missing?
$evalAsync
and$applyAsync
target to different situations.$evalAsync: defers the expression to the next loop iteration of current digest cycle. One Angular digest cycle loops for a few times until no data is dirty. If no digest cycle is in progress, it will start a new digest cycle (call
$apply
method) and evaluate the expression (call$eval
method) in it. This method is useful if you call a function out of Angular scope but still like to digest the dirty data when a digest cycle is already in progress, in which case you cannot call$apply
.$applyAsync: defers the expression to the next cycle of digest. It always starts a new cycle of digest to after the expression is evaluated (call
$apply
method). This method is useful if you frequently execute some service call back (like$http
service) out Angular scope with dirty scope data. However, if it starts a digest for each callback, there may be bad performance. Therefore, this method optimize the process by share the digest cycles among several async callbacks, which outperforms the method$evalAsync
.$applyAsync already exists:
evalAsync handles digests differently:
This behavior of this when called implicitly changed in AngularJS 1.3:
References
AngularJS API: $applyAsync
applyAsync vs evalAsync