1

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

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

4

1 回答 1

1

这是不可能的。

您只能注入.config以下项目:

  • 持续的
  • 价值
  • 提供者

请参阅此处的官方文档,我引用:

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

于 2015-06-01T18:39:31.673 回答