当 $scope.$watchCollection
用于更改对象中的属性时,它仅评估第一级属性。当调用 $scope.$watchCollection
时,有没有办法强制 angular 评估第二级或更低级别?
最佳答案
您正在寻找的是 $scope.$watch(watchExpression, listener, [objectEquality]);
其中第三个参数“objectEquality”为 true
- The watchExpression is called on every call to
$digest()
and should return the value that will be watched. (Since$digest()
reruns when it detects changes the watchExpression can execute multiple times per$digest()
and should be idempotent.)- The listener is called only when the value from the current
watchExpression
and the previous call towatchExpression
are not equal (with the exception of the initial run, see below). Inequality is determined according to reference inequality, strict comparison via the!==
Javascript operator, unlessobjectEquality == true
(see next point)- When
objectEquality == true
, inequality of thewatchExpression
is determined according to theangular.equals
function. To save the value of the object for later comparison, theangular.copy
function is used. This therefore means that watching complex objects will have adverse memory and performance implications. The watch listener may change the model, which may trigger other listeners to fire. This is achieved by rerunning the watchers until no changes are detected. The rerun iteration limit is 10 to prevent an infinite loop deadlock.
https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch
关于javascript - Angularjs $scope.$watchCollection 仅第一级属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590531/