これにはinterceptor
を使用する必要があります。ここでAngularJSのドキュメントからのお勧めの方法は次のとおりです。
// 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');
あなたはすべてのメソッドはオプションですと、'request'
メソッドを実装するだけです。提供されるconfigオブジェクトは、角度$http
の設定オブジェクトであり、headers
プロパティを含んでいます。あなたはこれに簡単に十分なあなたのヘッダを追加することができるはずです。
config.headers.myHeader = myValue;
return config;
あなたは、単に引数リストに追加することで、あなたのconfigブログで$httpProvider
をつかむことができます。
angular
.module('frontend', [
...
])
.config(function($routeProvider, $httpProvider, $provide){
$routeProvider
.when(...)
// register the interceptor as a service
$provide.factory('myHttpInterceptor', function() {
return {
// optional method
'request': function(config) {
config.headers.myHeader = myValue;
return config;
},
};
});
$httpProvider.interceptors.push('myHttpInterceptor');
});
スタートです(https://docs.angularjs.org/api/ng/provider/$httpProvider) – charlietfl