I have an array containing some tests, which I have to run sequentially.
Within an angular forEach loop, I'm iterating over the test array and calling the backend via http get to start the current test.
My problem is now, that the user should have the possibility to stop the test run, so that further tests will not be executed anymore.
In the view I have a "Stop test run" button, which sets a $scope.stopTest variable to true. Within my foreach loop I have a condition, which checks the $scope.stopTest variable on each iteration and if it's set to true skips the test run.
But it seems, that inside the loop angular does not recognize the scope variable change from false to true.
I had a test output within the loop which shows me that the value was still false.
In my view I have these buttons:
<input type="button" class="btn" ng-click="runTests()" value="Start test run" ng-disabled="disabled == true" />
<input type="button" class="btn" ng-click="stopRunning = true" value="Stop test run" />
In my controller I have this code:
$scope.runTests = function () {
angular.forEach($scope.selected, function (value, key) {
if (!$scope.stopRunning) { // PROBLEM: angular does not recognize that stopRunning has changed already
promise = promise.then(function () {
$scope.count++;
$scope.status = "Running test " + $scope.count + " of " + $scope.countSelectedTests + ".";
return $http({
method: 'GET',
url: 'api/runtest/' + key
}).then(function (res) {
$scope.testresults = res.data;
});
});
}
});
Does anyone know what is the problem here? Is it about child scopes or the digest cycle? I was trying to call $apply() or $digest() after changing the stopRunning variable, but with no effect.
How can I stop the loop by button click event?