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
Just use $evalAsync instead of $apply
.
You can use this statement:
if ($scope.$root.$$phase != '$apply' && $scope.$root.$$phase != '$digest') {
$scope.$apply();
}
If scope must be applied in some cases, then you can set a timeout so that the $apply is deferred until the next tick
setTimeout(function(){ scope.$apply(); });
or wrap your code in a $timeout(function(){ .. }); because it will automatically $apply the scope at the end of execution. If you need your function to behave synchronously, I'd do the first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With