David Dahan
David Dahan

Reputation: 11162

AngularJS: How to update $scope after a promise response?

Disclaimer: I'm a beginner with AngularJS.

I'm trying to update $scope after a promise has returned, but it does not work. Here's the simplified code:

.controller('ChooseRewardCtrl', ['$scope', ...,
                                  function($scope, ...) {

    // scope data
    $scope.points = CardModel.points;

    // function called to refresh points
    $scope.refresh_points = function() {
      return CardModel.refresh_points() // This is a promise that changes CardModel.points value
      .then(function(data){
        $scope.$apply() // Throw error (cf. above)
      })
    }
}

I get a Error: [$rootScope:inprog] $digest already in progress I saw lots of post about this error, but nothing that I can directly link to my problem.

Thanks!

Upvotes: 0

Views: 1740

Answers (2)

gupta
gupta

Reputation: 1

you need not use $apply().

log your function parameter ('data') and see what does it contain.

It should contain value returned by promise.

assign the appropriate value to your scope variable.

Upvotes: 0

CD..
CD..

Reputation: 74156

Try this:

.then(function(data){
        $scope.points = CardModel.points;
      })

Upvotes: 2

Related Questions