Steve Lorimer
Steve Lorimer

Reputation: 28659

AngularJs: resolving a promise doesn't update my scope

I am under the impression that binding to a promise in my html

<ul ng-repeat='i in items'>
    <li>{{i}}</li>
</ul>

(where $scope.items is the promise), that when the promise is resolved, the scope will automatically update.

var _deferred = $q.defer();

$scope.items = _deferred.promise;

$scope.setItems = function() {
  _deferred.resolve([
    'Here',
    'There',
    'Everywhere'
  ]);
};

When I call setItems(), the promise is resolved, but my html doesn't update.

What am I doing wrong?

Plunker here

Upvotes: 0

Views: 310

Answers (1)

dfsq
dfsq

Reputation: 193261

The way you are setting items is not correct. $scope.items should not be a Promise object, but you should use Promise then methods to set items:

_deferred.promise.then(function(items) {
    $scope.items = items;
});

Demo: http://plnkr.co/edit/4dtyniFayCHKrD1exWV1?p=preview

Upvotes: 3

Related Questions