如何在$观赏角度超过一个集? [英] How to $watch more than one collection in Angular?

查看:184
本文介绍了如何在$观赏角度超过一个集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序两个数组我想观察的变化:

I have two arrays in my application I would like to observe changes:

$scope.a = [{id: 1, text: 'test a - 1'}, ...];
$scope.b = [{id: 1, text: 'test b - 1'}, ...];

function changed(){
  // notify application about changes in a or b
}

我要叫改变()功能时, A B 已被更改。

$scope.$watchCollection('a', changed);
$scope.$watchCollection('b', changed);

改变()回调将被触发,如果两次我在 A 乙,以及对初始步骤。我想两块手表在一个结合,这样的事情:

The changed() callback will be triggered twice if I have changes in a and b as well as on init step. I would like to combine two watches in one, something like this:

$scope.$watchCollection('a, b', changed);

我怎样才能做到这一点有角?

How can I do this with Angular?

推荐答案

有一些所谓的 watchGroup 您可以使用用于此目的,但不幸的是就是因为它只是浅浅的手表大多是无用的。

There is something called watchGroup which you can use for this purpose, But unfortunately is is mostly useless as it does only shallow watch.

您可以使用观察者函数返回的对象

You could use the watcher function to return your object

$scope.$watch(function(){ //Or even try this with watchCollection
     return ['a','b'].map(angular.bind($scope, $scope.$eval));
  }, function(newV){
      console.log(newV);
  },true);

您可以改为 <一个href=\"http://stackoverflow.com/questions/25712888/how-to-use-a-watchgroup-with-object-equality-or-deep-watch-the-properties-in-th#answer-25713285\">create为了方便您的自定义观察家正如我在另一种答案提到 并做这样的事情。

You could instead create a custom watcher for your convenience as i had mentioned in another answer and do something like this.

$scope.deepWatchGroup(['a','b'],function(newV){
  console.log(newV);
});

这篇关于如何在$观赏角度超过一个集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆