Gearbox
Gearbox

Reputation: 336

Angularjs override http global interceptor

I am following the exact guide at http://www.ictit.com/en/blog/post/30/show-loading-screen-globally-and-centralize-in-ionic-angularjs/ to intercept http requests, I wonder if it is possible to override it by adding some kind of option variable when making $http request?

Upvotes: 2

Views: 2850

Answers (1)

Henry Zou
Henry Zou

Reputation: 1917

$httpProvider.interceptors.push(function($rootScope) {
  return {
    //http request show loading
    request: function(config) {
      if (!config.override) {
        $rootScope.$broadcast('loading:show')
      }
      return config
    },
    //hide loading in case any occurred
    requestError: function(response) {
      if (!config.override) {
        alert("requestError");

        $rootScope.$broadcast('loading:hide')
      }
      return response
    },
    //Hide loading once got response
    response: function(response) {

      if (!config.override) {
        $rootScope.$broadcast('loading:hide')
      }
      return response
    },
    //Hide loading if got any response error 
    responseError: function(response) {

      if (!config.override) {
        alert("responseError");
        $rootScope.$broadcast('loading:hide')
      }
      return response
    }
  }
})

In the $http call simply pass $http.get(url, {override: true})

Upvotes: 3

Related Questions