複数の$scope属性を監視する 質問する

複数の$scope属性を監視する 質問する

複数のオブジェクトのイベントをサブスクライブする方法はありますか?$watch

例えば

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

ベストアンサー1

AngularJS 1.3からは、新しいメソッドが追加されました。$watchGroup一連の表現を観察するため。

$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 
});

おすすめ記事