I'm using a service to grab some data from an API:
angular.module('myApp', [])
.factory('myService', function($q, $timeout) {
var getMessages = function() {
var deferred = $q.defer();
$timeout(function() {
deferred.resolve('Hello world!');
}, 2000);
return deferred.promise;
};
return {
getMessages: getMessages
};
});
And I use these data in multiple controllers.
function ControllerA($scope, myService) {
$scope.message = myService.getMessages();
$scope.updateMessage = function(){
$scope.message = 'Hello Max';
};
}
function ControllerB($scope, myService) {
$scope.message = myService.getMessages();
$scope.$watch('message', function(){
// 'Hello Max'
}, true);
}
I would like to update the data in every controller, but when I change the $scope.message in the ControllerA, it doesn't fire a change in the ControllerB.
EDIT: The thing is that I would like to avoid using "$broadcast" and "$on".
Any ideas?
Here's a jsfiddle: http://jsfiddle.net/Victa/McLQD/