我正在尝试将自定义标头注入每个 API 请求。当我提供一些硬编码文本时,它可以工作。
工作代码
myApp.config(['$httpProvider', function ($httpProvider) {
var requestInterceptor = ['$q', '$rootScope',
function ($q, $rootScope) {
var interceptorInstance = {
request: function (config) {
config.headers['X-MyApp-CustomHeader'] = "foobar";
return config || $q.when(config);
}
};
return interceptorInstance;
}];
$httpProvider.interceptors.push(requestInterceptor);
}]);
不工作的代码
myApp.config(['$httpProvider', function ($httpProvider) {
var requestInterceptor = ['$q', '$rootScope', '$route',
function ($q, $rootScope, $route ) {
var interceptorInstance = {
request: function (config) {
config.headers['X-MyApp-CustomHeader'] = $route.current.params.CustomParameter;
return config || $q.when(config);
}
};
return interceptorInstance;
}];
$httpProvider.interceptors.push(requestInterceptor);
}]);
错误,尝试注入时$route
未捕获的错误:[$injector:cdep] 找到循环依赖项:$route <- $http http://errors.angularjs.org/1.2.3/ $injector/cdep?p0=%24route%20%3C-%20% 24http