Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs watchGroup()无法处理服务更改_Angularjs_Watch - Fatal编程技术网

Angularjs watchGroup()无法处理服务更改

Angularjs watchGroup()无法处理服务更改,angularjs,watch,Angularjs,Watch,当其他控制器对服务进行某些更改时,我在监视服务中的多个变量时遇到问题。 我有以下建议: angular.module('carApp').controller('CarListCtrl', function ($scope, CarService) { $scope.carList = CarService.carList $scope.initalAmount = CarService.initialAmount $scope.startDate = CarServic

当其他控制器对服务进行某些更改时,我在监视服务中的多个变量时遇到问题。 我有以下建议:

angular.module('carApp').controller('CarListCtrl', function ($scope, CarService) {
    $scope.carList = CarService.carList
    $scope.initalAmount = CarService.initialAmount
    $scope.startDate = CarService.startDate

    $scope.$watchGroup(['carList', 'initialAmount', 'startDate'],
        function (newValues, oldValues, $scope) {
            console.log(newValues);
        });
});
其他控制器会一直更新服务中的值,但监视组从未启动

我已经创建了一个直接针对服务的简单手表,以检查它是否工作,并且正在工作…,因此我认为手表组应该直接针对服务变量,但我找不到如何做

这是一款简单的手表:

$scope.$watch(function () {
    return CarService.carList
}, function (newVal, oldVal) {
     console.log(newVal);
});
我应该怎么做才能使它和多个服务变量一起工作

更新1:

只是更新一下。。。如果我尝试只使用一个元素的watchgroup,例如$scope.$watchgroup(['carList'),…它可以工作,所以我尝试使用每一个元素,每次都可以工作,但只要再添加一个元素,它就会停止工作…非常烦人


Tks又来了,伙计们!

为了结束这篇文章,angularjs github的人帮了我一把:这是给需要帮助的人的anwser:

watchGroup数组中的每个值都可以是表达式或函数,因此您可以在watchGroup中使用三个不同的函数

您的第一个示例可能不起作用,因为您的其他控制器为您的服务中的initialAmount和startDate分配了新值,这意味着您的控制器和startDate中有不同值的不同对象。它可能与carList一起工作,因为您只添加/删除项目,这意味着它仍然有效控制器和服务中的同一对象

$scope.$watchGroup([
    function() {
      return myService.valueOne()
    },
    function() {
      return myService.valueTwo()
    }    
  ], function(newValues, oldValues) {
    $scope.valueOne = newValues[0]
    $scope.valueTwo = newValues[1]
  })