AngularJS $watch vs $watchCollection:这是更好的性能?

前端之家收集整理的这篇文章主要介绍了AngularJS $watch vs $watchCollection:这是更好的性能?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对于观察对象范围变量,是$ scope。$ watch with objectEquality设置为true OR $ scope。$ watchCollection更好?

对于一个$ scope对象变量(如15个属性,一些嵌套的2级深度)用视图中的输入元素和ng-model更新,$ scope有多糟糕$ watch with 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',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 ...

The $watchCollection() function is a sort-of mid-ground between the
two $watch() configurations above. It’s more in-depth than the
vanilla $watch() function; but,it’s not nearly as expensive as the
deep-equality $watch() function. Like the $watch() function,the
$watchCollection() works by comparing physical object references;
however,unlike the $watch() function,the $watchCollection() goes
one-level deep and performs an additional,shallow reference check of
the top level items in the collection.

see this explanation

猜你在找的Angularjs相关文章