This is a question I've seen been asked many times before but I can't seem to find a solution that works for me. I have a simple code that updates a loading
property and two divs, one that's supposed to be shown when loading is true and one that's shown when loading is false.
$scope.loading = true;
function getTasks() {
$http.get(URL)
.then(function (response) {
//success
$scope.tasks = response.data;
}
, function () {
//failure
$scope.errorMessage = "Failed to fetch tasks";
$timeout(getTasks(), 5000);
})
.finally(function () {
$scope.loading = false;
});
}
The HTML is as follows:
<div ng-show="{{ loading === true }}">
<i class="fa fa-spinner fa-w-16 fa-spin fa-lg"></i>
</div>
<div ng-show="{{ loading === false }}">
However, even though loading
does change to false, it doesn't update the view.
Some of the things I've tried are:
Using the timeout service
$timeout(() => { $scope.loading = false; });
Using
$scope.$apply()
both after changing loading to false and calling a function inside of apply.Adding the getTasks function to the scope.