Angular观察多个值,没有ng-repeat

时间:2015-07-21 07:35:06

标签: javascript angularjs watch

是否可以在同一$watch内观看多个$ scope值,而不使用ng-repeat? 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");
        }
    });
});

1 个答案:

答案 0 :(得分:0)

从角度1.3开始,您可以使用$scope.$watchGroup,如下所示:

$scope.$watchGroup(['prov1', 'prov2'], function (newValue, oldValue) {
    if (newValue != oldValue) {
        console.log("wee");
    }
});

小提琴: http://jsfiddle.net/wr4twkzm/1/