Different use cases, I have actually seen, and also where someone was trying to force a digest or an apply…

1. A finish use case

An use situation to know where is executed once in a cycle

// HTMLClickMe// Javascriptthis.click = function() console.debug('ng-click code executed'); $timeout(function() console.debug('$timeout code executed'); ); $scope.$evalAsync(function() console.debug('$scope.$evalAsync password executed'); ); // ERROR $scope.$apply(function() console.debug('$scope.$apply password executed'); );;

*

*

ng-click code begin a new cycle, bike 1We are currently in one $apply phase, for this reason if you speak to $apply() you will acquire an error and calling $digest() is useless because it’s the following phase (and you acquire the exact same error)$evalAsync is useless$timeout password will pressure Angular to develop a brand-new cycle, bike 2 (performance impact)

2. Third-party/DOM occasions use case

This one is THE most IMPORTANT one, all difficulties come native the lack of $apply contact when you room in the call back of a thirdparty or in the callback of a jquery/DOM event. An extremely often forgot, this is the main cause of a dramatic “… currently in progress” cascade

The good version

// HTML Click external Angular digest// Javascriptdocument.querySelector('#button1').onclick = function() console.debug('event click code executed'); $scope.$apply(function() console.debug('$scope.$apply password executed'); );;

*

*

DOM event click phone call our code (it might be any type of other DOM occasions or a thirdparty event/callback)As you deserve to see $scope.$apply execute a brand-new cycle v our password inside, it’s perfect, our code is correctly insecuted in Angular contextWe have both phase: $apply & $digestThings acquire worse…

So here, is an instance of something you should never do, utilizing $digest() rather of $apply()

// HTML Click outside Angular digest// Javascript// never ever DO THATdocument.querySelector('#button2').onclick = function() console.debug('event click password executed'); $scope.myValue = 2; //BAD $scope.$digest();;

*

The $apply() step is not executed! the why Angular strongly recommand to never ever use $digest().

You are watching: Apply already in progress

3. In a promise

Using $q, the Angular’s promise library, you are sure to be in the Angular context, so no need to $apply or $digest

// Javascriptthis.ngClick = function() console.debug('call promise') asyncGreet().then(function(greeting) console.debug('promise result received'); );
Angular develop a brand-new $digest just cycle, once the promise is resolved, so if you speak to $scope.$digest() or $apply() inside the promise deal with callback, you will acquire “$digest currently in progress”

3. In a watcher

Watchers are already in the Angular context, so…

Third party example

An example if you room in a thirdparty, or a DOM event.

// Javascript$scope.$watch('myValue', function(newValue) console.debug('$watcher myValue', newValue);)document.querySelector('#button2').onclick = function() console.debug('event click password executed'); $scope.myValue = 2; $scope.$apply();;
You call $apply in the onclick duty because you are out that the Angular context.In the watcher, never contact $apply() or $digest() since you space 100% of the moment in a cycleIn one angular context

I change the use case over with an ng-click, for this reason I’m currently in the Angular context give thanks to to ngClick directive.

See more: How To Plane Wood Without A Planer (Easy Tips And Tricks), Can You Plane Wood Without A Planer

// HTMLClick Me// Javascript $scope.$watch('myValue', function(newValue) console.debug('$watcher myValue', newValue);)this.ngClick = function() console.debug('code ng click') $scope.myValue = 3;;
ConclusionI hope this guide will help you to understand how you can totally avoid any type of “$digest/$apply currently in progress”.

$digest() is bad, really, never use it$apply(your code) is come execute code that are not executed in the Angular context, like third party library, events dom…$timeout, $evalAsync, … try to never ever use them and trust me, girlfriend canYou can avoid 90% that $scope.$digest()/$scope$apply(), the 10 rather are just for point #2Only one misused $apply() or $digest() and you start a dramatic cascade the “… already in progress” errors.Never never never NEVER try to use this negative thing the you deserve to see anywhere on internet where you try to address the Angular’s private property $$phase to execute a digest/apply if it’s not the present phase that angular… EEERRRKKKK