2016-09-12 10 views
0

私は、サービス変数が変更されるたびに関数を呼び出さなければならないという指示があります。

次のコードは、ディレクティブ内にある:

$rootScope.$watch('movesService.selectedMove', function() { 
    console.log(movesService.selectedMove); 
    if (movesService.selectedMove === "Punch") { 
     vm.pushToFightLog('Select Target'); 
    } 
    if (movesService.selectedMove === "Fury") { 
     //Action 
    } 
    if (movesService.selectedMove === "Fortify") { 
     //Action 
    } 
    if (movesService.selectedMove === "Parry") { 
     //Action 
    } 
    }, true); 

サービス:

angular 
    .module('outerZone') 
    .service('movesService', movesService); 

    function movesService() { 
    var vm = this; 

    vm.selectedMove = "Punch"; 


    } 

事$時計が初めて呼び出されたとき、変数とログを読むことができるということですそれはコンソールに表示されますが、それ以降は変数が変更されても機能を起動しません。

$ rootScopeが正しく注入されているとはかなり確信していますが、ここでは二重チェックするコードがあります。

angular 
    .module('outerZone') 
    .directive('fightDisplay', fightDisplay); 

    fightDisplay.$inject = ["alliesService", "enemiesService", "fightQueueService", "$timeout", "movesService", "$rootScope"]; 

    function fightDisplay(alliesService, enemiesService, fightQueueService, $timeout, movesService, $rootScope) { 
+0

は、私たちが作業を持つことができます確認したい場合は、プランカ?速く答えるのに役立つだろう! –

+0

私は本質的になぜこれが動作しないのだろうかhttps://plnkr.co/edit/YBrHJkblkZBmo4ozGusr?p=preview –

+0

答えはあなたのために働いたのですか? –

答えて

0

あなたが提供したplunkerを使って、以下のように時計機能をいくつか変更しました。

$scope.$watch('test', function() { //scope variable should be watched like this 
    $scope.watchCount++ 
});//removed true for strict checking (===) as $scope.test is initalised as String 'Hello' 

私たちは厳格な)作業plunker .TOは($見る方法の詳細を知っている。ここ$scope.test =0

$scope.$watch('test', function() { 
    $scope.watchCount++ 
    },true); 

を変更後、チェックの作品は、このblog

関連する問題