I’m finding that I need to update my page to my scope manually more and more since building an application in angular.
The only way I know of to do this is to call $apply() from the scope of my controllers and directives. The problem with this is that it keeps throwing an error to the console that reads :
Error: $digest already in progress
Does anyone know how to avoid this error or achieve the same thing but in a different way?
You can check if a
$digestis already in progress by checking$scope.$$phase.$scope.$$phasewill return"$digest"or"$apply"if a$digestor$applyis in progress. I believe the difference between these states is that$digestwill process the watches of the current scope and its children, and$applywill process the watchers of all scopes.To @dnc253’s point, if you find yourself calling
$digestor$applyfrequently, you may be doing it wrong. I generally find I need to digest when I need to update the scope’s state as a result of a DOM event firing outside the reach of Angular. For example, when a twitter bootstrap modal becomes hidden. Sometimes the DOM event fires when a$digestis in progress, sometimes not. That’s why I use this check.I would love to know a better way if anyone knows one.
From comments:
by @anddoutoi
angular.js Anti Patterns