如果我们有 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 if
$scope.myPlanet = 'Earth';
$scope.otherPlanet = ['Venus','Jupiter','Mars'];
以及如何访问每个变量值。
最佳答案
您仍然可以使用相同的,
$scope.$watchGroup(['myPlanet', 'otherPlanet'], function(newVals, oldVals, scope) {
$log.log(newVals, oldVals, scope);
});
<强> DEMO
关于javascript - 如何在 2 个变量(其中一个是数组)上使用 $watchgroup 或 $watchCollection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40206861/