gpt4 book ai didi

javascript - $watchCollection 和多个表达式不起作用

转载 作者:行者123 更新时间:2023-11-30 15:37:20 26 4
gpt4 key购买 nike

我有两个数组,每个数组都有 bool 值,我尝试使用 $watchCollecion 函数在其中一个数组发生某些更改时显示一条消息,但由于某种原因它不起作用。

我想看看这个 example 有什么问题

Controller

  $scope.arrayCategoryA = [];
$scope.arrayCategoryB = [];

$scope.$watchCollection(['arrayCategoryA', 'arrayCategoryB'], function(newVal, oldVal, scope){
console.log("something changed");
}, true);

$http.get("categoryA.json").success(function(data) {
$scope.categoryA = data;
for (var i = 0; i < $scope.categoryA.length; i++)
$scope.arrayCategoryA[i] = true;
});

$http.get("categoryB.json").success(function(data) {
$scope.categoryB = data;
for (var j = 0; j < $scope.categoryB.length; j++)
$scope.arrayCategoryB[j] = true;
});

最佳答案

AngularJS has always had a Scope.$watch() function as means to observe [and react to] changes in a given value. With AngularJS 1.1.4, however, they added the Scope.$watchCollection() function as a means to observe changes in a collection (either as an Array or an Object).

$watchCollection 仅适用于一个数组或对象。因此您必须将代码更改为单一监视。

$scope.$watchCollection('arrayCategoryA', function(newVal, oldVal){
console.log("something changed 1");
}, true);
$scope.$watchCollection('arrayCategoryB', function(newVal, oldVal){
console.log("something changed 2");
}, true);

关于javascript - $watchCollection 和多个表达式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41322088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com