1

I am fetching a data with this function in my controller:

var fetchStoreItems = function () {
    return $http.get('/enterprises/_store').then(function (response) {
        $scope.items = response.data;
    }, function (errResponse) {
        console.error("Error while fetching data")
    })
};

It is working fine and fast enough. But what if there are lots of items to fetch?! I want to put a spinner while data is being fetched.

How can I do this?

2 Answers 2

3

In your controller,

var fetchStoreItems = function () {
    $scope.loading = true;            //define a `$scope` variable; show loading image when ajax starts
    return $http.get('/enterprises/_store').then(function (response) {
        $scope.items = response.data;
        $scope.loading = false;       //hide loading image  when ajax successfully completes
    }, function (errResponse) {
        console.error("Error while fetching data");
        $scope.loading = false;      //hide loading image  when ajax error
    })
};

<img src="pathToLoadingImage" ng-show="loading" />  // show the loading image according to `$scope.loading`
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to use it for some of the rest API calls, kalhano's answer works well. However if you want spinner for all your angular http calls, you might consider using an interceptor.

Please check this link for that: Http call docs with interceptors explained

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.