0
我有一個返回一個承諾對象服務(公用):
.service('Utilities',function($q){
var self = this;
var deferred = $q.defer();
..... function thats calls self.filteredDataGlobal.....
self.getFilteredData = function(){
if(self.filteredDataGlobal){
deferred.resolve(self.filteredDataGlobal);
}
return deferred.promise;
};
});
在我控制我所說的承諾對象時,它的準備,其中工程..
.controller('myController', function($scope, Utilities) {
var self = this;
Utilities.getFilteredData().then(function(data){
self.filteredData = data;
});
//watch for when self.filteredDataGlobal changes on the Utilities service
$scope.$watch(self.filteredData, function (newVal, oldVal) {
console.log(newVal, oldVal);
self.filteredData = newVal;
});
});
......但我試圖「監視」self.filteredDataGlobal何時更改實用程序服務。現在這是日誌記錄undefined,undefined
好帖子在這裏:[http://stackoverflow.com/questions/12576798/angularjs-how-to-watch-service-variables](http://stackoverflow.com/questions/12576798/angularjs-how-to -watch-service-variables) – jjwilly16
使用$ broadcast。這裏回答你的問題http://stackoverflow.com/a/20863597/2003481 – kraken