在Angular js中真的找不到有關http攔截器的好文檔。雖然通過ng-include
致毒處理錯誤,我可以利用這個攔截responseError
:
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('templateInterceptor');
});
// register the interceptor as a service
app.factory('templateInterceptor', function($q) {
return {
'responseError': function(rejection) {
var isTemplate = !!rejection.config.url.match(/^content/g);
if (isTemplate) {
// here we add error message, but how this message appesrs in the place of ng-include
rejection.data = '<div><template-error url="\''+ (rejection.config.url) + '\'"><strong>Error from interceptor.</strong></template-error></div>';
return rejection;
} else {
return $q.reject(rejection);
}
}
}
});
此代碼從這個問題how to catch angular ng-include error拍攝。 我不明白,攔截器是如何工作的?他們必須返回什麼?如何使用rejection
傳遞給responseError
攔截器的參數?在rejection
data
屬性用於包含錯誤消息到失敗ng-include
指令的地方,這是如何工作的?
你看過https://docs.angularjs.org/api/ng/service/$http#interceptors – 2014-10-26 19:04:36