手表除了一个对象的所有元素 [英] Watch all elements of an object except one
本文介绍了手表除了一个对象的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表在我的code
I have a watch in my code
scope.$watch(foo, function () {
...
}, true);
这确保了如果在对象foo改变任何属性,那么这款手表将被调用。我想打一个例外。我想打电话给这款腕表如果除了一个foo的改变任何属性。如果该属性改变了这种手表应该不会被调用。这怎么可能?
This ensures that if any attribute in the object foo changes then this watch will be called. I want to make an exception to this. I want to call this watch if any attribute in foo changes except one. If that attribute changes this watch should not be called. How is this possible?
推荐答案
我能想到的两种不同的方式来做到这一点:
I can think of 2 different ways to do this:
选项1,只是处理一下你的 $观看
函数的开头:
Option 1, just handle it at the beginning of your $watch
function:
scope.$watch(foo, function (newVal, oldVal) {
if(newVal.propertyThatYouDontWantToWatch === oldVal.propertyThatYouDontWantToWatch)
return;
/* Normal Code here*/
}, true);
选项2,定义你不想看这样的(我是pretty确保此选项将不会触发 $观看
你的富
的对象):
Object.defineProperty(
foo,
'propertyThatYouDontWantToWatch',
{
enumerable:false,
configurable:true,
writable: true,
value:{} /*Replace {} with the value that you want to assign to your property*/
}
);
这篇关于手表除了一个对象的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文