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

标签 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 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/

相关文章:

javascript - 如何处理动态内容的点击事件?

javascript - 未处理的拒绝错误: Can't set headers after they are sent using Node

node.js - 允许使用 Yeoman (1.0) grunt 服务器进行跨域调用

html - 需要的消息显示在带有 ui-select2 的页面顶部

javascript - JavaScript Angular 项目中的 TypeScript - 参数 'SearchCtrl' 不是函数或未定义

javascript - 使用 Lazysizes RIaS 插件有条件地更改数据大小的输出

javascript - "TypeError: d is undefined"在带有鼠标悬停事件的条形图上转换时

javascript - 具有多个参数的 Angular 输入滤波器

javascript - iframe跨域获取CSS

javascript - Promise 未在 meteor-angular 应用程序中定义