I'm trying to use a service to share data between two controllers. But I'm not seeing the data update after changing the data with XHR.
angular
.module('appy', [])
.service('MyService', function($http){
return {
dataToChange: {
},
ajaxy: function(url){
var self = this;
setTimeout(function(){
console.log('now we are in the service!');
self.dataToChange = 'something new';
console.log(this.dataToChange);
}, 1000);
}
};
})
.controller('c1', ['$scope', 'MyService', function($scope, MyService){
$scope.myService = MyService;
$scope.ajaxy = function(){
$scope.myService.ajaxy();
};
}])
.directive('dirOne', function(){
return {
restrict: 'E',
templateUrl: 'dirone-temp.html'
};
})
.controller('c2', ['$scope', 'MyService', function($scope, MyService){
$scope.myData = MyService.dataToChange;
}])
.directive('dirTwo', function(){
return {
restrict: 'E',
templateUrl: 'dirtwo-temp.html'
};
});
But nothing happens when ajaxy
makes his update. I tried setting this up like recommended by this question. No joy. I also tried adding a watch.
$scope.$watch(function(){
return MyService.ajaxy();
}, function(oldVal, newVal){
$scope.somethingToChange = newVal;
console.log($scope.somethingToChange);
});
But nothing happens still. Also this object is pretty big. I would prefer not to $watch
it.