2016-09-12 5 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

당신이 제공 한 플러 커로 다음과 같이 시계 기능을 일부 변경했습니다.

$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' 

우리)는 엄격한는 $scope.test =0하고 여기에

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

는 $ (시청하는 방법을 더 알고 .TO plunker을하고있다 변경 한 후 확인하려면이 blog

관련 문제