dman
dman

Reputation: 11064

Angular.js Two Digest() Sucessions Clashing

I have this function:

function scopeMutateSuccess(modalInstance, state) {
    $rootScope.$apply(function() {
      modalEventConfig.statusType = 'success';
      modalEventConfig.nodeStatus = 'Completed Successfully';
    });
}

In the call stack...there a is a digest() that is called before this function. However, it is still running while this function gets called and I get a digest() already in progress. I need my own digest() in this function or otherwise the view does not get updated with the scope member values. The first digest() is being called somewhere else and not by me so I can't controller it firing.

Is there a event listener for when a digest() ends or a way to keep them from clashing?

Upvotes: 0

Views: 37

Answers (1)

Shomz
Shomz

Reputation: 37701

You can always check for digest in progress using $scope.$$phase (returns true if it's in progress).

However, the simplest fix here seems to be just to wrap the inside function into a $timeout callback (which takes care of the in-progress issue for you), like this:

function scopeMutateSuccess(modalInstance, state) {
    $timeout(function() {
      modalEventConfig.statusType = 'success';
      modalEventConfig.nodeStatus = 'Completed Successfully';
    }, 0);
}

Make sure you inject the $timeout dependency, of course.

Upvotes: 2

Related Questions