Some well-known criticisms and flaws. They all involve a very common problem in the $digest loop (update cycle): How do I update $scope outside of angular?
Where do you call $apply?
It is a better practice to make sure that you are calling $apply outside of the $digest loop, such as in the "link" Directive (Directive) response function.
That is, you can only call $apply outside of $controller, so that it can access the declaration code of HTML or other controller controllers-for example, Apply invokes the link directive and triggers the corresponding function, which is certainly outside of the $digest loop .
How do I handle errors that are thrown outside of Angularjs?
$scope. $apply () receives a function or angular expression string, executes it, and then calls $scope. $digest () to update all bindings or listeners.
If your code is not wrapped in a function passed to $apply, it throws an error, and the error is thrown outside of Angularjs, which means that the error handling mechanism in your app cannot handle it. $apply not only executes your code, but its internal try/catch statement keeps your error captured, and $digist is the last guarantee that it will work even if a bug is thrown. That's pretty good.
Therefore, we should change the expression beyond angular and update $scope within the $apply.
Summarize
I hope you now know when and how to use $apply (). If you only need ANGULARJS to provide you with the functionality, then the probability you use it is not high. However, if you write a custom directive and manipulate the DOM element directly , it is necessary to understand it.
Angular $apply () and $digest () Tutorial 1