javascript - Trying to $watch for a variable changing on a service -


i have service (utilities) returns promise object:

.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;     };  }); 

in controller call promise object when it's ready, works...

.controller('mycontroller', function($scope, utilities) {     var self = this;      utilities.getfiltereddata().then(function(data){         self.filtereddata = data;     });      //watch when self.filtereddataglobal changes on utilities service     $scope.$watch(self.filtereddata, function (newval, oldval) {         console.log(newval, oldval);         self.filtereddata = newval;     }); }); 

...but i'm trying "watch" when self.filtereddataglobal changes on utilities service. right logging undefined, undefined

$scope.$watch watches variables on scope. saved variable in context of utility not scope. maybe pass scope in , store global value on scope can watch it? or have setter gets called , can emit event?


Comments