# The interceptors of $httpProvider doesn't work for native app?

**URL:** <https://forum.ionicframework.com/t/the-interceptors-of-httpprovider-doesnt-work-for-native-app/21640>\
**Category:** ionic-v1\
**Created:** [April 24, 2015, 1:36pm UTC](https://forum.ionicframework.com/t/the-interceptors-of-httpprovider-doesnt-work-for-native-app/21640 "2015-04-24T13:36:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin\_lu](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/justin_lu/32/80448_2.png) [@Justin\_lu](https://forum.ionicframework.com/u/Justin_lu)\
**Post date:** [April 24, 2015, 1:36pm UTC](https://forum.ionicframework.com/t/the-interceptors-of-httpprovider-doesnt-work-for-native-app/21640/1 "2015-04-24T13:36:24Z")

</div>

I want to show loading-mark when each request was started and hide the loading-mark automatically after the request was completed.  
There are my code:

```javascript
  $ionicPlatform.ready(function() {
      $rootScope.$on('loading:show', function() {
         $ionicLoading.show({template: 'Loading...'})
      });
      $rootScope.$on('loading:hide', function() {
        $ionicLoading.hide()
      });
  }
    angular.module('abx.routes', []).config(function($httpProvider) {
      $httpProvider.interceptors.push(['$q', '$location', '$rootScope', function($q, $location, $rootScope) {
      return {
        request: function(config) {
          $rootScope.$broadcast('loading:show');
          config.headers = config.headers || {};
          if (localStorage.access_token) {
            // config.headers.Authorization = 'Bearer ' + $localStorage.token;
            config.headers.access_token = localStorage.access_token;
          }
          return config;
        },
        response: function(response){
          $rootScope.$broadcast('loading:hide');
          return response;
        },
        responseError: function(response) {
          if (response.status === 401 || response.status === 403) {
            alert('no access token error!');
          }
          return $q.reject(response);
        }
      };
    }]);
  })

```

It’s work for me on the chrome. But doesn’t work on the emulate.  
Am I right?

---

<div class="post-metadata">

**Author:** ![seanhill](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/seanhill/32/105765_2.png) [@seanhill](https://forum.ionicframework.com/u/seanhill)\
**Post date:** [April 24, 2015, 3:44pm UTC](https://forum.ionicframework.com/t/the-interceptors-of-httpprovider-doesnt-work-for-native-app/21640/2 "2015-04-24T15:44:12Z")

</div>

Hey @Justin_lu,

In my project I started off doing the same thing, but realized that when you have multiple requests firing off to your server, the `request` and `response` callbacks are constantly being called, and so your `loading:show` and `loading:hide` events are being called often, thus your loader has a constant cycle of

```
show 
show
hide
hide
show
hide

```

Also, you may not want to show the loader on every request because then your user would constantly be having the loader flash in their face, which isn’t a great user experience. The solution I came up with would be something like this:

```
$rootScope.$broadcast('loading:show');

User.get().then(function(user){
	$scope.user = user;
}).finally(function(){
	$rootScope.$broadcast('loading:hide');
});

```

That way you show your loader only on requests you desire, perhaps on ones that take a while. In my project I show the loader after a `400ms` delay which you can specify with

```
$ionicLoading.show({ template: 'Loading...', delay: 400 });

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Justin\_lu](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/justin_lu/32/80448_2.png) [@Justin\_lu](https://forum.ionicframework.com/u/Justin_lu)\
**Post date:** [April 27, 2015, 6:59am UTC](https://forum.ionicframework.com/t/the-interceptors-of-httpprovider-doesnt-work-for-native-app/21640/3 "2015-04-27T06:59:59Z")

</div>

Thank a lot. It’s awesome.
