Angularjs覆盖了http全局拦截器

时间:2016-01-02 18:57:43

标签: javascript angularjs

我正在关注http://www.ictit.com/en/blog/post/30/show-loading-screen-globally-and-centralize-in-ionic-angularjs/的确切指南来拦截http请求,我想知道是否可以通过在发出$ http请求时添加某种选项变量来覆盖它?

1 个答案:

答案 0 :(得分:3)

$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
    }
  }
})

在$ http电话中,只需传递$http.get(url, {override: true})

即可