javascript - Angularjs $scope.$watchCollection 仅第一级属性

标签 javascript angularjs

$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 to watchExpression 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, unless objectEquality == true (see next point)
  • When objectEquality == true, inequality of the watchExpression is determined according to the angular.equals function. To save the value of the object for later comparison, the angular.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/

相关文章:

javascript - 在 iOS 和 OSX 上的强制网络助手 (WISPr) 中运行 angularJS

javascript - Angular 不适用于 Express 和 Pug

javascript - 需要删除对象数组中的重复项 - 获取键的较高值

javascript - 在 Meteor 的钩子(Hook)中更改 react 变量的值

javascript - 使用 AngularJS 服务构建 html 部分

javascript - 如何在有条件的 ng-click 上执行多个调用?

javascript - ng-repeat 循环中的 ng-change 增量和减量值

javascript - 如何使用 Angular 将 UTC 时间转换、显示和编辑为浏览器本地时间?

javascript - 值不与数组对象绑定(bind)

javascript - 使用angularjs恢复默认下拉值