Quoted directly from the official Angular documentation:
The $apply() function serves to execute an expression within the angular framework but triggered from outside of it. This could be from various sources such as browser DOM events, setTimeout, XHR requests, or external libraries. As we are interfacing with the Angular framework externally, it is necessary to manage the scope life-cycle, handle exceptions, and run watches appropriately.
Furthermore, a pseudo-code representation of $apply is provided:
function $apply(expr) {
try {
return $eval(expr);
} catch (e) {
$exceptionHandler(e);
} finally {
$root.$digest();
}
}
In essence, the $apply
method evaluates an expression and initiates a digest cycle, prompting Angular to execute all watch listeners and update any bound views accordingly.
To clarify, although you mentioned using $apply
for updating model bindings, this is primarily essential when updates originate externally to Angular. Typically, manual invocation is not required in most scenarios.