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