2
\$\begingroup\$

Here is my code to add a progress bar on every HTTP request. Can someone please point out to me whether I am doing it right or not?

Template for loader:

<div class="loader" ng-class="!loaderState ? 'hidden' : ''">
 <div class="parentLoader">
 <div class="loaderContainer">
 <div class="logo"></div>
 <div class="loadingMsg">Processing..., Please Wait...</div>
 <div class="loadingImg"></div>
 </div>
 </div>
</div>

Interceptor:

.factory('authInterceptor', ['$rootScope', function($rootScope){
 return {
 request : function request (config) { 
 $rootScope.loaderState = true;
 return config;
 },
 response : function response(response) {
 $rootScope.loaderState = false;
 return response;
 }
 };
 }])
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 4, 2016 at 11:31
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Yes, u can do like this. But some scenario doesn't work.

Scenario 1: If controller has 5 parallel services, at that time loader will get ended after any response received in out of 5.

Scenario 2: We don't need to show loader to some of the services, internally we will call based on the click or any action. At that time it will fail.

Instead of using interceptor better use that flag in respective controller itself or u have to pass additional flag in the header to hide and show loader via interceptor. My suggestion is handle that in respective controller itself.

alecxe
17.5k8 gold badges52 silver badges93 bronze badges
answered Apr 10, 2017 at 18:43
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.