在angularjs中添加拦截器,发现$http发出的请求会拦截,但$window.location.href确不会拦截,想请问一下拦截器是不是只拦截$http发出的请求?
为情所困2017-05-15 17:15:17
The official document explains clearly and has examples
https://docs.angularjs.org/ap...$http
// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {
// optional method
'request': function(config) {
// do something on success
return config;
},
// optional method
'requestError': function(rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
},
// optional method
'response': function(response) {
// do something on success
return response;
},
// optional method
'responseError': function(rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push('myHttpInterceptor');
// alternatively, register the interceptor via an anonymous factory
$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
return {
'request': function(config) {
// same as above
},
'response': function(response) {
// same as above
}
};
});
曾经蜡笔没有小新2017-05-15 17:15:17
The so-called $window
其实是对浏览器 window
secondary packaging of object references, so why is there this thing? The purpose is mainly for code testability.
So, the conclusion is that this thing has nothing to do with $http, and naturally it will not use interceptors
Of course, I still understand the title of the question, but I just hope to do some extra things when making jumps. This problem can only be solved from the perspective of routing.
Above!