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