Issue:
I am facing a challenge where I want to pass an array of variables into $watchGroup and iterate through the array to update the values of each element. However, the current approach I am using does not seem to be effective:
$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];
}
}
});
Currently, I'm using the following workaround:
$scope.$watchGroup(['stopWatchService.secondsElapsed', 'stopWatchService.minutesElapsed', 'stopWatchService.colon'], function(newValues, oldValues, scope){
scope.secondsElapsed = newValues[0];
scope.minutesElapsed = newValues[1];
scope.colon = newValues[2];
});
Any suggestions or alternative approaches to address this issue?