如何在2个变量上使用$ watchgroup或$ watchCollection,其中一个是数组?

时间:2016-10-23 19:17:25

标签: javascript angularjs

如果我们有2个变量,那么我们可以使用$ watchCollection或$ watchgroup,如下所示。

$scope.firstPlanet = 'Earth';
$scope.secondPlanet = 'Mars';

$scope.$watchCollection('[firstPlanet, secondPlanet]', function(newValues){
  console.log('*** Watched has been fired. ***');
  console.log('New Planets :', newValues[0], newValues[1]);
});

如果

我们如何使用$ watchCollection
$scope.myPlanet = 'Earth';  
$scope.otherPlanet = ['Venus','Jupiter','Mars'];

以及如何访问每个变量值。

1 个答案:

答案 0 :(得分:1)

你仍然可以使用相同的,

 $scope.$watchGroup(['myPlanet', 'otherPlanet'], function(newVals, oldVals, scope) {
    $log.log(newVals, oldVals, scope);
});

<强> DEMO

相关问题