2016-01-02 146 views

回答

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

+0

在requestError,響應和responseError,配置是不確定的。但是如果你使用'if(!response.config.override){...}',它會起作用。 – JanP

+0

如果我們有帖子? – walox

+1

@walox'''$ http.post(url,data,{override:true})''' –