$apply()
is used to execute an expression in angular from outside of the angular framework.
(For example from browser DOM events, setTimeout, XHR or third party libraries).
Because we are calling into the angular framework we need to perform proper scope life-cycle
of exception handling
,
executing watches
.
$apply()
function $apply(expr) {
try {
return $eval(expr);
} catch (e) {
$exceptionHandler(e);
} finally {
$root.$digest();
}
}
Scope's $apply()
method transitions through the following stages:
$eval()
method.$exceptionHandler
service.watch
listeners are fired immediately after the expression
was executed using the $digest()
method.angular.scope.$apply([exp]);
exp(optional) – {(string|function())} –
An angular expression to be executed.
string
: execute using the rules as defined in expression.function(scope)
: execute the function with current scope
parameter.{*}
– The result of evaluating the expression.