Angular 观察多个值,没有 ng-repeat
Angular watch multiple values, without ng-repeat
是否可以在不使用 ng-repeat 的情况下在同一个 $watch
中观察多个 $scope 值? Fiddle
<div ng-app="myapp">
<div ng-controller="ctrlParent">
<input type="checkbox" ng-model="prov1">
<input type="checkbox" ng-model="prov2">
<div>{{prov1}}</div>
<div>{{prov2}}</div>
</div>
</div>
var app = angular.module('myapp', []);
app.controller('ctrlParent', function ($scope) {
$scope.prov1 = true;
$scope.prov2 = true;
$scope.ids = {};
$scope.$watch('prov1', function (newValue, oldValue) {
if (newValue != oldValue) {
console.log("wee");
}
});
});
从 angular 1.3 开始,您可以按如下方式使用 $scope.$watchGroup
:
$scope.$watchGroup(['prov1', 'prov2'], function (newValue, oldValue) {
if (newValue != oldValue) {
console.log("wee");
}
});
Fiddle:
http://jsfiddle.net/wr4twkzm/1/
是否可以在不使用 ng-repeat 的情况下在同一个 $watch
中观察多个 $scope 值? Fiddle
<div ng-app="myapp">
<div ng-controller="ctrlParent">
<input type="checkbox" ng-model="prov1">
<input type="checkbox" ng-model="prov2">
<div>{{prov1}}</div>
<div>{{prov2}}</div>
</div>
</div>
var app = angular.module('myapp', []);
app.controller('ctrlParent', function ($scope) {
$scope.prov1 = true;
$scope.prov2 = true;
$scope.ids = {};
$scope.$watch('prov1', function (newValue, oldValue) {
if (newValue != oldValue) {
console.log("wee");
}
});
});
从 angular 1.3 开始,您可以按如下方式使用 $scope.$watchGroup
:
$scope.$watchGroup(['prov1', 'prov2'], function (newValue, oldValue) {
if (newValue != oldValue) {
console.log("wee");
}
});
Fiddle: http://jsfiddle.net/wr4twkzm/1/