2016-06-01 90 views
0

我的AngularJS 1.3中有以下手表。应用:AngularJS两款互相影响的手表

$scope.$watch('vm.reportInstitution', function(newValue, oldValue) { 
     if(oldValue != newValue) { 
      userService.getAllUsersOfInstitutionByInstitutionId(vm.reportInstitution.id).then(function successCallback(response) { 
       vm.optionsUser = response.data; 
       vm.reportUser = vm.optionsUser.length > 0 ? vm.optionsUser[0] : null; 
      }, function errorCallback(response) { 
        console.log('error in recordReport.controller.js#watch#reportInstitution'); 
      }); 
     } 
    }); 

    $scope.$watch('vm.reportUser', function(newValue, oldValue) { 
     if(oldValue != newValue) { 
      userService.getInstitutionsOfUser(vm.reportUser.id).then(function successCallback(response) { 
       vm.optionsInstitution = response.data; 
       vm.reportInstitution = vm.optionsInstitution.length > 0 ? vm.optionsInstitution[0] : null; 
      }, function errorCallback(response) { 
        console.log('error in recordReport.controller.js#watch#reportUser'); 
      }); 
     } 
    }); 

因此,相互影响,例如,如果vm.reportInstitution变化超过vm.reportUser设置,并且如果vm.reportUser更改,则会设置vm.reportInstitution - >这会产生无限循环。

我现在的问题是,如果我可以通过阻止传播类似来防止这种情况?

回答

0

可以停止/与stopwatchreportInstitution()/startwatchreportInstitution()这样启动守护进程:

var stopwatchreportInstitution = null ; 
var stopwatchreportUser = null ; 

function startwatchreportInstitution(){ 
    stopwatchreportInstitution = $scope.$watch('vm.reportInstitution', function(newValue, oldValue) { 
     if(oldValue != newValue) { 
      userService.getAllUsersOfInstitutionByInstitutionId(vm.reportInstitution.id).then(function successCallback(response) { 
       vm.optionsUser = response.data; 

       // STOP the other watch 
       if (stopwatchreportUser != null) { 
        stopwatchreportUser();// arrete le watcher 
        stopwatchreportUser = null; 
       } 

       vm.reportUser = vm.optionsUser.length > 0 ? vm.optionsUser[0] : null; 
      }, function errorCallback(response) { 
        console.log('error in recordReport.controller.js#watch#reportInstitution'); 
      }); 
     } 
    }); 
} 
startwatchreportInstitution() ; 

function startwatchreportUser(){ 
    stopwatchreportUser = $scope.$watch('vm.reportUser', function(newValue, oldValue) { 
     if(oldValue != newValue) { 
      userService.getInstitutionsOfUser(vm.reportUser.id).then(function successCallback(response) { 
       vm.optionsInstitution = response.data; 

       // STOP the other watch 
       if (stopwatchreportInstitution != null) { 
        stopwatchreportInstitution();// arrete le watcher 
        stopwatchreportInstitution = null; 
       } 

       vm.reportInstitution = vm.optionsInstitution.length > 0 ? vm.optionsInstitution[0] : null; 
      }, function errorCallback(response) { 
        console.log('error in recordReport.controller.js#watch#reportUser'); 
      }); 
     } 
    }); 
} 
startwatchreportUser(); 

编辑:看官方文档中的“返回此侦听注销功能。” https://docs.angularjs.org/api/ng/type/ $ rootScope.Scope

+0

感谢您的回应。之后是否有可能开始观看。目前的行为是,如果我改变机构,而不是只有这块手表适用于进一步的变化和vv。 ?谢谢! – quma

+0

我编辑了我的答案。我不明白你的意思她现在的行为是,如果我改变机构,只有这块手表适用于进一步的变化和vv。 ? “对不起 – AlainIb