有没有办法组合多个AngularJS手表?

有没有办法组合多个AngularJS手表?,angularjs,Angularjs,我有这个: $scope.$watch('option.sCreatedBy', function (newValue, oldValue) { if (newValue && newValue !== oldValue) { _u.oyc.put('sCreatedBy', newValue); $scope.grid.data = null; }

我有这个:

        $scope.$watch('option.sCreatedBy', function (newValue, oldValue) {
            if (newValue && newValue !== oldValue) {
                _u.oyc.put('sCreatedBy', newValue);
                $scope.grid.data = null;
            }
        });

        $scope.$watch('option.sModifiedBy', function (newValue, oldValue) {
            if (newValue && newValue !== oldValue) {
                _u.oyc.put('sModifiedBy', newValue);
                $scope.grid.data = null;
            }
        });
所有这些功能都是一样的,还有一些我在上面没有介绍的功能


有没有一种方法可以将这些手表组合成一个手表并同时观看/修改多个东西?

现在,您正在创建两个相同的匿名函数,然后将它们传递给每个
$watch
方法。第二个参数只是一个回调函数,因此您不妨命名回调函数并传递引用

var watchCallback = function (newValue, oldValue) {
    if (newValue && newValue !== oldValue) {
        _u.oyc.put('sCreatedBy', newValue);
        $scope.grid.data = null;
    };
};
$scope.$watch('option.sCreatedBy', watchCallback);
$scope.$watch('option.sModifiedBy', watchCallback);
另一种可能更具角度感的方法是使用
$watchCollection
而不是
$watch

$scope.$watchCollection('[option.sCreatedBy, option.sModifiedBy]', function (newValue, oldValue) {
    if (newValue && newValue !== oldValue) {
        _u.oyc.put('sCreatedBy', newValue);
        $scope.grid.data = null;
    };
});

需要注意的是,
$watchCollection
的第一个参数不是数组,而是看起来像数组的字符串。

现在,您正在创建两个相同的匿名函数,然后将它们传递给每个
$watch
方法。第二个参数只是一个回调函数,因此您不妨命名回调函数并传递引用

var watchCallback = function (newValue, oldValue) {
    if (newValue && newValue !== oldValue) {
        _u.oyc.put('sCreatedBy', newValue);
        $scope.grid.data = null;
    };
};
$scope.$watch('option.sCreatedBy', watchCallback);
$scope.$watch('option.sModifiedBy', watchCallback);
另一种可能更具角度感的方法是使用
$watchCollection
而不是
$watch

$scope.$watchCollection('[option.sCreatedBy, option.sModifiedBy]', function (newValue, oldValue) {
    if (newValue && newValue !== oldValue) {
        _u.oyc.put('sCreatedBy', newValue);
        $scope.grid.data = null;
    };
});

需要注意的是,
$watchCollection
的第一个参数不是数组,而是看起来像数组的字符串。

现在,您正在创建两个相同的匿名函数,然后将它们传递给每个
$watch
方法。第二个参数只是一个回调函数,因此您不妨命名回调函数并传递引用

var watchCallback = function (newValue, oldValue) {
    if (newValue && newValue !== oldValue) {
        _u.oyc.put('sCreatedBy', newValue);
        $scope.grid.data = null;
    };
};
$scope.$watch('option.sCreatedBy', watchCallback);
$scope.$watch('option.sModifiedBy', watchCallback);
另一种可能更具角度感的方法是使用
$watchCollection
而不是
$watch

$scope.$watchCollection('[option.sCreatedBy, option.sModifiedBy]', function (newValue, oldValue) {
    if (newValue && newValue !== oldValue) {
        _u.oyc.put('sCreatedBy', newValue);
        $scope.grid.data = null;
    };
});

需要注意的是,
$watchCollection
的第一个参数不是数组,而是看起来像数组的字符串。

现在,您正在创建两个相同的匿名函数,然后将它们传递给每个
$watch
方法。第二个参数只是一个回调函数,因此您不妨命名回调函数并传递引用

var watchCallback = function (newValue, oldValue) {
    if (newValue && newValue !== oldValue) {
        _u.oyc.put('sCreatedBy', newValue);
        $scope.grid.data = null;
    };
};
$scope.$watch('option.sCreatedBy', watchCallback);
$scope.$watch('option.sModifiedBy', watchCallback);
另一种可能更具角度感的方法是使用
$watchCollection
而不是
$watch

$scope.$watchCollection('[option.sCreatedBy, option.sModifiedBy]', function (newValue, oldValue) {
    if (newValue && newValue !== oldValue) {
        _u.oyc.put('sCreatedBy', newValue);
        $scope.grid.data = null;
    };
});

重要的是要注意,
$watchCollection
的第一个参数不是数组,而是一个看起来像数组的字符串。

可能重复的可能重复的可能重复的可能重复的$watchCollection看起来不错,但我如何知道更改了哪个值。你的答案是:_.oyc.put('sCreatedBy',newValue);但是如果sModified by是触发手表的东西呢?谢谢$watchCollection看起来不错,但我如何知道哪个值已更改。你的答案是:_.oyc.put('sCreatedBy',newValue);但是如果sModified by是触发手表的东西呢?谢谢$watchCollection看起来不错,但我如何知道哪个值已更改。你的答案是:_.oyc.put('sCreatedBy',newValue);但是如果sModified by是触发手表的东西呢?谢谢$watchCollection看起来不错,但我如何知道哪个值已更改。你的答案是:_.oyc.put('sCreatedBy',newValue);但是如果sModified by是触发手表的东西呢?谢谢