在函数中调用服务在config()中在AngularJS中

时间:2015-06-01 18:32:57

标签: angularjs restangular angular-local-storage

我在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()以便能够访问本地存储中的数据,以包含我所做的每个Restangular请求。

PS:我知道在config()或run()中,你只能调用提供者或常量。我想知道setFullRequestInterceptor()中的函数中的代码是否只能在运行时调用,可以以某种方式实例化或获取localStorageService吗?

1 个答案:

答案 0 :(得分:1)

这是不可能的。

您只能向.config注入以下内容:

  • CONSTANT
  • VALUE
  • PROVIDER

请参阅官方文档here,我引用:

  

配置块 - 在提供商注册期间执行   和配置阶段。只能注入提供者和常量   到配置块。这是为了防止意外实例化   服务完全配置之前的服务。