1

I want to "refresh" my view after some new data is loaded into my factory, but i cant figure out how to do this correctly.

$http.post(url, postKunde).success(function(data, status) {
                $rootScope.calc = data.calc;
                $rootScope.$apply();
        }).error(function(data, status) {
                console.log(status);
        });

This is the method Im using in my factory. And everytime it triggers i get this error:

Error: $digest already in progress

Can anyone tell me how to do this correctly ? Thanks ...

4
  • 1
    possible duplicate of Error: $digest already in progress Commented May 17, 2013 at 14:38
  • Can you just get rid of the $rootScope.$apply() call? Commented May 17, 2013 at 14:42
  • yeah but then the changes on the data don't apply on the view. Commented May 17, 2013 at 15:06
  • 1
    $http callbacks are wrapped in a call to $apply() (or they call $apply), so you shouldn't need to call $apply() here. Commented May 17, 2013 at 16:00

1 Answer 1

4

You could write a safe apply that will check if there is a digest:

 $scope.safeApply = function(fn) {
  var phase = this.$root.$$phase;
  if(phase == '$apply' || phase == '$digest') {
    if(fn && (typeof(fn) === 'function')) {
      fn();
    }
  } else {
    this.$apply(fn);
  }
};

Reference for this little gem: https://coderwall.com/p/ngisma

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that was a huge help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.