加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:31:52 所属栏目:安全 来源:网络整理
导读:对于观察对象范围变量,是$ scope。$ watch with objectEquality设置为true OR $ scope。$ watchCollection更好? 对于一个$ scope对象变量(如15个属性,一些嵌套的2级深度)用视图中的输入元素和ng-model更新,$ scope有多糟糕$ watch with objectEquality设
对于观察对象范围变量,是$ 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读