我在一个AngularJS应用程序中有这个config()块:
.config(function(RestangularProvider, apiServerConstants) {
RestangularProvider.setBaseUrl(apiServerConstants.url + apiServerConstants.apiPath + apiServerConstants.apiVersion);
RestangularProvider.setFullRequestInterceptor(function(element, operation, route, url, headers, params, httpConfig) {
// access local storage
return {
element: element,
params: _.extend(params, {single: true}),
headers: headers,
httpConfig: httpConfig
};
});
});如何在RestangularProvider.setFullRequestInterceptor()调用的函数中注入我的localStorageService (从grevory/angular-local-storage)。我需要配置拦截器函数,以便它能够调用localStorageService.get(),以便能够访问本地存储中的数据,并将其包含在我发出的每个重定向请求中。
PS:我知道在config()或run()中,你只能调用提供者或常量。我想知道只在运行时调用的setFullRequestInterceptor()函数中的代码是否能以某种方式实例化或获取localStorageService?
发布于 2015-06-02 02:39:32
这是不可能的。
您只能向.config中注入以下项目:
参见官方文档here,我引用如下:
配置块-在提供商注册和配置阶段执行。只有提供程序和常量才能注入到配置块中。这是为了防止服务在完全配置之前意外实例化。
https://stackoverflow.com/questions/30580703
复制相似问题