Javascript Angular$watchCollection旧值未针对多次更新进行正确更新
我有一个html文件,其中有3个复选框值Javascript Angular$watchCollection旧值未针对多次更新进行正确更新,javascript,angularjs,watch,Javascript,Angularjs,Watch,我有一个html文件,其中有3个复选框值 <input type="checkbox"ng-model="checkbox-1"/> <input type="checkbox"ng-model="checkbox-2"/> <input type="checkbox"ng-model="checkbox-3"/> 但是,当checkboc值被一次又一次地更新时,$watchCollection的旧值列表没有正确更新,并且一直用旧值填充 e、 g 所有复选
<input type="checkbox"ng-model="checkbox-1"/>
<input type="checkbox"ng-model="checkbox-2"/>
<input type="checkbox"ng-model="checkbox-3"/>
但是,当checkboc值被一次又一次地更新时,$watchCollection的旧值列表没有正确更新,并且一直用旧值填充
e、 g
- 所有复选框最初都取消勾选
- 勾选第一个复选框->oldValue=[false,false,false],newValue=[true,false,false]
- 现在勾选第二个复选框->oldvalue=[false,false,false],newValue=[true,true,false]
<div ng-app="valueList" ng-controller="myCtrl">
<input type="checkbox" ng-model="checkBoxes.checkbox1"/>
<input type="checkbox" ng-model="checkBoxes.checkbox2"/>
<input type="checkbox" ng-model="checkBoxes.checkbox3"/>
</div>
尝试添加以下内容:
$scope.$watchGroup(['checkbox-1', 'checkbox-2', 'checkbox-3'], function (newValue, oldValue) {
console.log('checkbox-1: new - ' + newValue[0] + ' old - ' + oldValue[0]);
console.log('checkbox-1: new - ' + newValue[1] + ' old - ' + oldValue[1]);
console.log('checkbox-1: new - ' + newValue[2] + ' old - ' + oldValue[2]);
},true);
",true)
如果你想深入了解它的工作原理,请阅读以下内容:
更新:
这是angular 1.6中确定的问题,他们将在1.7版本中解决此问题
你能提供一个提琴或工作片段,我可以在其中进行测试吗?更新了JsFiddle-@SameeraSM,我没有发现任何问题。我遗漏了什么吗?我已经为你更新了小提琴,它起作用了。我认为你的问题一开始是错的,你先改变了值1,然后你改变了值2为什么你认为第一个的旧值应该改变?
var app = angular.module("valueList", []);
app.controller("myCtrl", function ($scope) {
$scope.checkBoxes = {};
$scope.$watchGroup(['checkBoxes.checkbox1', 'checkBoxes.checkbox2', 'checkBoxes.checkbox3'], function (newValue, oldValue) {
console.log('=======================');
console.log('checkbox-1: old - ' + oldValue[0] + ' ,new - ' + newValue[0]);
console.log('checkbox-1: old - ' + oldValue[1] + ' ,new - ' + newValue[1]);
console.log('checkbox-1: old - ' + oldValue[2] + ' ,new - ' + newValue[2]);
});
});
$scope.$watchGroup(['checkbox-1', 'checkbox-2', 'checkbox-3'], function (newValue, oldValue) {
console.log('checkbox-1: new - ' + newValue[0] + ' old - ' + oldValue[0]);
console.log('checkbox-1: new - ' + newValue[1] + ' old - ' + oldValue[1]);
console.log('checkbox-1: new - ' + newValue[2] + ' old - ' + oldValue[2]);
},true);
",true)