早上好,我遇到了一个我自己似乎无法解决的问题。
我正在尝试在与初始配置块不同的位置注册一个 httpinterceptor。
这样做可以按预期工作:
angular.module('app', ['ngResource'])
.config(function($httpProvider) {
$httpProvider.interceptors.push(function() {
return {
request: function($request) {
console.log($request);
return $request;
}
}
})
})
但是拦截器是子模块的一部分。所以我的应用程序如下所示:
编辑:
angular.module('app', ['ngResource']);
angular.module('app.submodule', ['ngResource'])
.config(function($httpProvider) {
$httpProvider.interceptors.push(function() {
return {
request: function($request) {
console.log($request);
return $request;
}
}
})
})
此外,子模块是延迟加载的。但我不认为这是问题的根源。
我尝试通过添加以下内容来引用初始配置块中的 httpProvider:
$httpProviderReference = $httpProvider;
所以我可以稍后注册一个拦截器。这甚至在最初的 run() 块中都行不通。检查提供者,它似乎有效,但从未调用过拦截器。
有谁知道如何解决这个问题?我正在尝试将身份验证令牌添加到请求的标头中。
提前感谢,奥卢