2016-08-09 6 views
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が変更されたときを「監視」しようとしています。今のところこれは未定義のログです。未定義

+0

ここに良い投稿:[http://stackoverflow.com/questions/12576798/angularjs-how-to-watch-service-variables](http://stackoverflow.com/questions/12576798/angularjs-how-to -watch-service-variables) – jjwilly16

+0

$ブロードキャストを使用します。あなたの質問への回答は次のとおりです:http://stackoverflow.com/a/20863597/2003481 – kraken

答えて

0

$ scope。$ watchはスコープ内の変数のみを監視します。スコープではなくユーティリティのコンテキストにその変数を保存しました。スコープにスコープを渡してスコープにグローバルな値を格納すると、それを見ることができますか?または、セッターが呼ばれてイベントを出すことができますか?

関連する問題