攔截器在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