0
我想根据重复中的其他值更改数值。我做了一只手表,但事实是,如果我改变了手表内的新值,手表又被触发了。如果新值发生变化,触发角度手表
有没有办法解决这个问题
我的收藏方式:
$scope.$watch('objlist', function(newValue, oldValue) {
$scope.counter += 1;
for (var count = 0; count < newValue.length; ++count) {
if (typeof newValue[count] != "undefined" && (typeof oldValue == "undefined" || newValue[count].value1 != oldValue[count].value1)) {
newValue[count].value3 = newValue[count].value1 * newValue[count].value2;
newValue[count].value3 = Number(newValue[count].value3).toFixed(2);
}
if (typeof newValue[count] != "undefined" && (typeof oldValue == "undefined" || newValue[count].value3 != oldValue[count].value3)) {
newValue[count].value1 = newValue[count].value3/newValue[count].value2;
newValue[count].value1 = Number(newValue[count].value1).toFixed(2);
}
}
}, true);
我已经包含了一个plunker向你展示我的意思。
,你可以看到,如果你改变字段1或3的值它全自动加2到计数器,并将两对toFixed(2)。这使得难以编辑字段。
我们使用角度1.2.25
只是一个提示,但你可以用newValue [count]替换typeof newValue [count]!=“undefined”。它应该这样做。测试一个undefined与说false – sam 2014-11-06 10:21:11
相同,因为你在$ watch中把最后一个参数作为'true',这意味着这个手表是深度对象监视。您无法更改正在观看的对象。 – Chandermani 2014-11-06 10:21:14
他可以为列表中的每个元素添加一个手表,分开。当然,如果他不知道元素的数量,那么不可用。 – sam 2014-11-06 10:22:12