我可以结合看多领域为一体的腕表AngularJS? [英] Can I combine watching of multiple fields into one watch with AngularJS?
问题描述
有没有办法在AngularJS这一合并成一个$手表或者我还需要一个$手表每一件事情我看?
$范围。$腕表('option.selectedContentType',函数(){
$ scope.getData();
}); $范围。$腕表('option.selectedContentStatus',函数(){
$ scope.getData();
});
您可以在下面找到关于如何使用$一些变化watchCollection
http://jsbin.com/IYofiPi/4 - 的此处工作的例子。
检查您的console.log看到被解雇事件。
看着一个数组项
$ scope.cities = ['萨尔瓦多,伦敦,苏黎世,里约热内卢]; //数组$范围。$ watchCollection('城市',函数(newValues,oldValues){
的console.log('***看了一下已经被解雇***。');
的console.log('新名称:',newValues);
});
看着一个对象的属性
$ scope.city = {
名称:伦敦,
国家:英格兰,
人口:8.174
}$范围。$ watchCollection(城市,功能(newValues,oldValues){
的console.log('***看了一下已经被解雇***。');
的console.log('新名称:',newValues);
});
观看范围变量列表($ scope.firstPlanet,$ scope.secondPlanet)
$ scope.firstPlanet ='地球';
$ scope.secondPlanet ='火星';$范围。$ watchCollection('[firstPlanet,secondPlanet]',函数(newValues){
的console.log('***看了一下已经被解雇***。');
的console.log('新的行星:',newValues [0],newValues [1]);
});
从AngularJS 1.3开始,有一个名为 $ watchGroup 观察新方法一组前pressions的。
块引用>Is there a way in AngularJS to combine this into the one $watch or do I still need a $watch for each thing I watch?
$scope.$watch('option.selectedContentType', function () { $scope.getData(); }); $scope.$watch('option.selectedContentStatus', function () { $scope.getData(); });
解决方案You can find below some variations on how to use $watchCollection
http://jsbin.com/IYofiPi/4 - working example here.
Check your console.log to see the event being fired.
Watching items of an Array
$scope.cities = ['Salvador', 'London', 'Zurich', 'Rio de Janeiro']; //Array $scope.$watchCollection('cities', function(newValues, oldValues) { console.log('*** Watched has been fired. ***'); console.log('New Names :', newValues); });
Watching properties of an Object
$scope.city = { name: 'London', country: 'England', population: 8.174 } $scope.$watchCollection('city', function(newValues, oldValues) { console.log('*** Watched has been fired. ***'); console.log('New Names :', newValues); });
Watching a list of scopes variables ($scope.firstPlanet, $scope.secondPlanet)
$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]); });
Starting from AngularJS 1.3 there's a new method called $watchGroup for observing a set of expressions.
这篇关于我可以结合看多领域为一体的腕表AngularJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!