In the event that you invoke $scope.apply
while the digest
function is already running, this situation may arise. It is probable that your scope.apply is unnecessary and can be omitted.
According to Angular Docs: $digest()
Processes all of the watchers of the current scope and its children. Typically, you do not call $digest()
directly in controllers or directives.
Angular keeps track of the processing phase, with key phases being $apply and $digest. Attempting to reenter a $digest or $apply while one is currently in progress usually signifies a programming error that needs attention. Angular will generate an error notification when this occurs. Find more information here
Your code resembles an older technique that was required in earlier versions of angular(0.4 or 0.8) but is likely not necessary in the version of angular you are utilizing.
The Angular Team has provided a method to analyze these types of errors:
When encountering this error, pinpointing the cause can be challenging. The recommended approach is to examine the stack trace associated with the error. Look for instances where $apply or $digest have been called and determine the context in which this occurred.
There will typically be two calls:
The initial call is the proper $apply/$digest triggered by an event near the top of the call stack.
The subsequent call is the problematic $apply/$digest that requires investigation.
Once this call has been identified, navigate up the stack to identify the issue.
If the second call originates from your application code, investigate why it was invoked within a $apply/$digest. It could be an oversight or it might align with the sync/async scenario discussed previously.
If the second call is within an Angular directive, it likely matches the second programmatic event trigger scenario outlined earlier. In this case, explore further up the chain to determine the initial event trigger.