gpt4 book ai didi

javascript - 将数组传递给 $watchGroup 并在更改时更新数组

转载 作者:行者123 更新时间:2023-11-28 07:56:15 26 4
gpt4 key购买 nike

问题:

我想将变量数组传递到 $watchGroup 并循环遍历该数组以更新每个变量的值,但这似乎不起作用:

$scope.secondsElapsed = stopWatchService.secondsElapsed;
$scope.minutesElapsed = stopWatchService.minutesElapsed;
$scope.colon = stopWatchService.colon;

$scope.timers = ['stopWatchService.secondsElapsed', 'stopWatchService.minutesElapsed', 'stopWatchService.colon'];

$scope.$watchGroup([$scope.timers], function(newValues, oldValues, scope){
if (newValues && newValues !== oldValues){
for (var i=0; i<newValues.length; i++){
scope.timers[i] = newValues[i];
}
}
});

我目前的解决办法如下:

$scope.$watchGroup(['stopWatchService.secondsElapsed', 'stopWatchService.minutesElapsed', 'stopWatchService.colon'], function(newValues, oldValues, scope){
scope.secondsElapsed = newValues[0];
scope.minutesElapsed = newValues[1];
scope.colon = newValues[2];
});

有什么想法吗?

最佳答案

您检查过$watchCollection吗? https://docs.angularjs.org/api/ng/type/ $rootScope.Scope#$watchCollection

关于javascript - 将数组传递给 $watchGroup 并在更改时更新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26044668/

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