简体   繁体   中英

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!

Try this:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM