看多$ scope属性 [英] Watch multiple $scope attributes

查看:237
本文介绍了看多$ scope属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来订阅使用多个对象的事件 $观看

例如

  $ $范围手表('项目1,ITEM2',函数(){});


解决方案

从AngularJS 1.3开始有一个所谓的新方法<一href=\"https://docs.angularjs.org/api/ng/type/%24rootScope.Scope#%24watchGroup\"><$c$c>$watchGroup观察一组前pressions的。

  $ scope.foo ='富';
$ scope.bar ='吧';$范围。$ watchGroup(['富','酒吧'],功能(newValues​​,oldValues​​,范围){
  // newValues​​数组包含手表前pressions的当前值
  //与指标相匹配那些watchEx pression阵
  //即
  // newValues​​ [0] - GT; $ scope.foo
  //和
  // newValues​​ [1] - &GT; $ scope.bar
});

Is there a way to subscribe to events on multiple objects using $watch

E.g.

$scope.$watch('item1, item2', function () { });

解决方案

Starting from AngularJS 1.3 there's a new method called $watchGroup for observing a set of expressions.

$scope.foo = 'foo';
$scope.bar = 'bar';

$scope.$watchGroup(['foo', 'bar'], function(newValues, oldValues, scope) {
  // newValues array contains the current values of the watch expressions
  // with the indexes matching those of the watchExpression array
  // i.e.
  // newValues[0] -> $scope.foo 
  // and 
  // newValues[1] -> $scope.bar 
});

这篇关于看多$ scope属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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