拦截器在documentation here。
下面是如何编写一个示例。
.config([
'$httpProvider',
function($httpProvider) {
var interceptor = [
'$q',
'$rootScope',
'userSession',
function($q, $rootScope, userSession) {
var service = {
// run this function before making requests
'request': function(config) {
if (config.method === 'GET' || userSession.isAuth()) {
// the request looks good, so return the config
return config;
}
// bad request, so reject
return $q.reject(config);
}
};
return service;
}
];
$httpProvider.interceptors.push(interceptor);
}
])
的原因也没什么可说的拦截器的$httpProvider
文档页面上是因为开发商没有包括在$http
script which the docs are generated from下面的代码:
一般
/**
* @ngdoc property
* @name $httpProvider#interceptors
* @description
// etc
文件被称为是不完整,不准确,和/或令人困惑。直到最近,当我找不到或理解某些东西时,我一直认为我是问题,但是我发现它通常是因为文档太糟糕了。但是,我们应该感谢我们拥有如此好用的工具,并且记住,文档可能很差,因为时间必须集中在编写工具而不是该工具的手册上。
最可靠的“文件”是源代码本身,尽管它可能不太友好阅读!在上面链接的源代码中,您可以看到this.interceptors = []
。 this
指的是$httpProvider
,因此它将属性interceptors
指定为$httpProvider
,该值为空数组。要添加拦截器,只需将拦截器push()
添加到此数组中。
来源
2014-10-03 02:07:45
m59
下面是关于拦截器的一个很好的解读:[AngularJS中的拦截器和有用的示例](http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/)。 – alecxe 2014-10-03 01:21:23
我以前已经阅读过这篇文章,但是它没有回答我的问题,请仔细阅读我的问题,我的问题看起来像是一个普遍的误解,而不是与拦截器相关的特定问题,谢谢。 – webberpuma 2014-10-03 01:25:35
我不想回答 - 这是一条评论。 – alecxe 2014-10-03 01:31:38