我有以下代码:
!(function(window, angular){
'use strict';
angular.module('interceptor', ['settings']).
config(function($httpProvider, $injector){
var $http,
interceptor = ['$q', '$injector', function ($q, $injector) {
var error;
function success(response) {
var $http = $http || $injector.get('$http');
var $timeout = $timeout || $injector.get('$timeout');
var $rootScope = $rootScope || $injector.get('$rootScope');
var LOADER_CONF = LOADER_CONF || $injector.get('LOADER_CONF');
if($http.pendingRequests.length < 1) {
$timeout(function(){
if($http.pendingRequests.length < 1){
$rootScope._loading = false;
}
}, LOADER_CONF.SUSPEND_ON);
}
else{
$timeout(function(){
if($http.pendingRequests.length > 0){
$rootScope._loading = true;
}
}, LOADER_CONF.SUSPEND_OFF);
}
return response;
}
function error(response) {
var $state = $state || $injector.get("$state");
var $timeout = $timeout || $injector.get('$timeout');
var $rootScope = $rootScope || $injector.get('$rootScope');
var LOADER_CONF = LOADER_CONF || $injector.get('LOADER_CONF');
$timeout(function(){ $rootScope._loading = false, LOADER_CONF.SUSPEND_OFF});
return $q.reject(response);
}
return function (promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
});
})(window, window.angular);
切换微调器的基本拦截器。
拦截器适用于获取部分和内部角度内容的 http 请求,但不适用于 RestAngular 请求(它是 $http 的包装器)。
任何帮助深表感谢。