1

I am just submitting one form.Its taking some time to return the response,on that time I need to show the loader.
This is my code

<div id="page-loading">
            <img style="display:none; margin:0 auto;"  src="~/images/loader.gif" />
        </div>

Script:

$scope.myFunc = function (leave) {
                            $scope.leaveDetails=leave;
                            console.log($scope.leaveDetails)
                            var requestHeaders = {
                                "content-type": 'application/json'
                            }
                            var httpRequest = {
                                method: 'POST',
                                url: '/Admin/sendRequest',
                                headers: requestHeaders,
                                data: $scope.leaveDetails
                            }
                            $http(httpRequest).then(function (response) {
                                $timeout(function () {
                                   $('#page-loading').fadeOut('slow');
                                }, 1000);

                                alert("successfully applied")
                                window.location= "/admin/Home";
                            })
                        }

But its not displaying the loader But taking more time to return the response.

If I give display:block here, <img style="display:none; margin:0 auto;" src="~/images/loader.gif" /> by default loader is coming but I need to show it only on submit of form.
Any suggestion?

2
  • You can use scope variable to show and hide image using ng-if or ng-show. Commented Jun 22, 2017 at 6:36
  • Please use angular in your angular application, not that jquery DOM stuff. It will save you a lot of trouble like this Commented Jun 22, 2017 at 6:38

3 Answers 3

3

Set a variable to scope like $scope.loading = true at the top of your method, and then in finally handler of http promise set it to false.

$http(httpRequest).then(...).finally(function() { $scope.loading = false; })

Based on that flag you can hide and show your loader.

<div id="page-loading" ng-if="loading">
    <img style="margin:0 auto;"  src="~/images/loader.gif" />
</div>
Sign up to request clarification or add additional context in comments.

5 Comments

why do I use .finally
In case if your request fails, you will want to hide the loader as well, won't you?
As soon as you redirect somewhere else, $timeout is extraneous there.
Create a plunker, and tell what's wrong with it, and we'll try to help you fix it.
2

I would use ng-if to show and hide the loader:

<div ng-if="loaderVisible">
     <img margin:0 auto;"  src="~/images/loader.gif" />
</div>

Then you can make loaderVisible = true in the beginning of your function and hide it again by making it false during the callback success.

Comments

1

You can use angular-busy below is link,

https://github.com/cgross/angular-busy

It's very simple to use no need of this all and if loader you not like then you can change loader CSS als

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.