我有一个物体的 watch 。当对象的属性更改时,监视会触发,除非属性名称以美元 ($) 开头。
考虑这段代码:
$scope.data = {};
$scope.$watch('data', function (newValue, oldValue) {
console.log('fired', newValue);
}, true);
和 html:
<div>
this will fire the event
<input type="text" ng-model="data.x" />
</div>
<div>
this won't
<input type="text" ng-model="data.$" />
</div>
你可以在这个 fiddle 中亲眼看到它: https://jsfiddle.net/gtrwzsn1/179/
这对我来说非常重要,因为我必须使用带有 $ 的属性名称来进行过滤。
最佳答案
AngularJs 有“$watchCollection”函数作为观察集合(即数组或对象)中变化的方法。
$scope.$watchCollection('data', function (newValue, oldValue) {
console.log('fired', newValue);
});
如果您想观看一个物体,请尝试上面的代码片段。希望这会有所帮助。
关于javascript - $watch 不会触发带有美元 ($) 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44010230/