$ scope。$ watch未调用,而模型已更改

时间:2016-09-18 13:00:56

标签: angularjs

我的$ scope。当调用vm.loadStats时,不会触发$ watch

var vm = this;

vm.loadStats = function(){
   vm.propositions = null;
   DateUtils.convertLocalDateToServer(vm.dateDebut);
   vm.dateFinSever = DateUtils.convertLocalDateToServer(vm.dateDebut);
   vm.propositions = PropositionsAffaireBetweenPropositionDates.get({dateDebut :     vm.dateDebutServer, dateFin : vm.dateFinSever});
}


$scope.$watch(['vm.propositions'], function(newValues, oldValues) {
...
}

如果有人知道为什么......
感谢

2 个答案:

答案 0 :(得分:0)

Angular $ scope对象有三个$ watch方法:

  • $ watch,接受字符串或函数

  • $ watchGroup,采用一系列表达式

  • $ watchCollection,拿一个对象

您应该使用$scope.$watch('vm.propositions', ...);$scope.$watchGroup(['vm.propositions'], ...);

AngularJS Documentation on $scope

答案 1 :(得分:0)

您的代码没有问题......这是一个小修复

//Note how I dropped "vm." prefix? its redundant because the strings you provide
//inside the array are translated into $scope.[yourScopedVariable]
$scope.$watch(['propositions'], function(newValues, oldValues) {
...
}