【发布时间】:2014-12-19 13:17:04
【问题描述】:
对于观察对象范围变量,$scope.$watch 和 objectEquality 设置为 true 还是 $scope.$watchCollection 更好?
对于使用输入元素和视图中的ng-model 更新的$scope 对象变量(如15 个属性,一些嵌套2 级深),$scope.$watch 与objectEquality 设置为true 的情况有多糟糕?这是要避免的大事吗?
$watchCollection 是更好的解决方案吗?
我正在寻找简单的方法来提高我的 AngularJS 应用程序的性能(我仍然停留在 v1.2.2 上)。
// ctrl scope var
$scope.filters = {
name: '',
info: {test: '', foo: '', bar: ''},
yep: ''
// etc ...
}
// ctrl watch ?
$scope.$watch('filters', function(newVal, oldVal) {
if(newVal !== oldVal) {
// call with updated filters
}
}, true);
// or ctrl watch collection ?
$scope.$watchCollection('filters', function(newVal, oldVal) {
if(newVal !== oldVal) {
// call with updated filters
}
});
// view input with ng-model
<input type="text" ng-model="filters.name" />
<input type="text" ng-model="filters.info.test" />
<input type="text" ng-model="filters.yep" />
// etc ...
【问题讨论】:
标签: angularjs angularjs-scope angularjs-watch