gpt4 book ai didi

javascript - AngularJS : $scope. $watch 一个对象并只返回它改变的属性

转载 作者:行者123 更新时间:2023-11-29 18:07:30 24 4
gpt4 key购买 nike

我正在做一些简单的范围观察,例如:

$scope.$watch('myObject', function(newValue, oldValue) {
if (newValue !== oldValue) {
return newValue;
}
}, true);

其中 myObject 是具有多个属性的普通对象。我只想返回更改的属性,即,如果有一个属性发生更改,比如 myObject.changedProperty,我只想返回它。

但我想监视整个对象(因此,我不必为每个属性设置不同的监视)。如何才能做到这一点?

谢谢!

最佳答案

$watchCollection 做你想做的。 ( $rootScope.Scope )

$watchCollection(obj, listener);

Shallow watches the properties of an object and fires whenever any of the properties change (for arrays, this implies watching the array items; for object maps, this implies watching the properties). If a change is detected, the listener callback is fired.

The obj collection is observed via standard $watch operation and is examined on every call to $digest() to see if any items have been added, removed, or moved. The listener is called whenever anything within the obj has changed. Examples include adding, removing, and moving items belonging to an object or array.

关于javascript - AngularJS : $scope. $watch 一个对象并只返回它改变的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30205873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com