Stack trace:
Error: $apply already in progress
at Error ()
at beginPhase (file:///android_asset/www/built.min.js:7:22740)
at Object.Scope.$a
You can use this statement:
if ($scope.$root.$$phase != '$apply' && $scope.$root.$$phase != '$digest') {
$scope.$apply();
}
Just use $evalAsync instead of $apply
.
We can use setTimeout
function in such cases.
console.log('primary task');
setTimeout(function() {
console.log('secondary task');
}, 0);
This will make sure that secondary task will be executed when execution of primary task is finished.
You are getting this error because you are calling $apply
inside an existing digestion cycle.
The big question is: why are you calling $apply
? You shouldn't ever need to call $apply
unless you are interfacing from a non-Angular event. The existence of $apply
usually means I am doing something wrong (unless, again, the $apply happens from a non-Angular event).
If $apply
really is appropriate here, consider using a "safe apply" approach:
https://coderwall.com/p/ngisma
At any point in time, there can be only one $digest
or $apply
operation in progress. This is to prevent very hard to detect bugs from entering your application. The stack trace of this error allows you to trace the origin of the currently executing $apply
or $digest
call, which caused the error.
More info: https://docs.angularjs.org/error/$rootScope/inprog?p0=$apply
Just resolved this issue. Its documented here.
I was calling $rootScope.$apply
twice in the same flow. All I did is wrapped the content of the service function with a setTimeout(func, 1)
.