2

I have a strange behaviour of my promise. See following plunk: http://plnkr.co/edit/uBuBS3Oi7mIPob2jtqkm

It is based on plunk of a very good article from Marc Galgleish (http://markdalgleish.com/2013/06/using-promises-in-angularjs-views/).

In my controller I have a simple $scope attribute named messages (getting a defer object from the service):

angular.module('myModule', [])
.controller('HelloCtrl', function($scope, HelloWorld) {
  $scope.messages = HelloWorld.getMessages();

  $scope.click = function()
  {
    alert($scope.messages[0]);
  }
});

As expected, after the promise is fulfilled (per resolve) the view gets the actual values and is showing them. But clicking on the button and executing the click function, I cannot use the $scope.messages as expected. The promise is not resolved there, just in the view.

With (http://plnkr.co/edit/vX3dSFjzB6R7q4Oed7Gj)

HelloWorld.getMessages().then( function(data){
  $scope.messages = data;
});

everything works fine, but that's not a solution for me. In my real application it's a service returning either a complex already synchronized, cached object (from local storage) or a promise if synchronization with the server is required. Return after synchronization the updated object.

Any ideas?

Thx in advance Knut

1 Answer 1

3

The promise is still a promise even after it is resolved.

Try this:

$scope.click = function()
{
  $scope.messages.then(function(msgArray) {
    alert(msgArray[0]);
  });
}
Sign up to request clarification or add additional context in comments.

Comments

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.