在应用程序配置中的自定义提供程序中使用 $http,angular.js [英] use $http inside custom provider in app config, angular.js

查看:23
本文介绍了在应用程序配置中的自定义提供程序中使用 $http,angular.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要问题 - 有可能吗?我试过没有运气..

主 app.js

<代码>...var app = angular.module('myApp', ['services']);app.config(['customProvider', function (customProvider) {}]);...

提供者自己

var services = angular.module('services', []);services.provider('custom', function ($http) {});

我遇到了这样的错误:

未捕获的错误:未知提供者:来自服务的 $http

有什么想法吗?

谢谢!

解决方案

底线是:

  • 不能将服务注入提供程序配置部分.
  • 可以将服务注入到初始化提供者服务的部分.

详情:

Angular 框架有一个 2 阶段的初始化过程:

阶段 1:配置

config 阶段,所有提供程序都被初始化,并且所有 config 部分都被执行.config 部分可能包含配置提供者对象的代码,因此它们可以被注入提供者对象.但是,由于提供者是服务对象的工厂,并且在此阶段提供者尚未完全初始化/配置 -> 在此阶段您不能要求提供者为您创建服务 -> 在配置阶段您不能使用/注入服务.此阶段完成后,所有提供程序都已准备就绪(配置阶段完成后无法再进行提供程序配置).

阶段 2:运行

run 阶段,所有run 部分都被执行.在这个阶段提供者已经准备好并且可以创建服务 -> 在 run 阶段你可以使用/注入服务.

示例:

1.将 $http 服务注入到提供者初始化函数 不会 工作

<块引用>

//错误angular.module('myModule').provider('myProvider', function($http) {//第 1 部分:初始化/配置 PROVIDER 的代码在这里(在 `config` 阶段执行)...this.$get = function() {//初始化/配置 SERVICE 的代码放在这里(在 `run` 阶段执行)返回我的服务;};});

由于我们试图将 $http 服务注入到在 config 阶段执行的函数中,我们将得到一个错误:

<块引用>

未捕获的错误:未知提供者:来自服务的 $http

这个错误实际上是说用于创建 $http 服务的 $httpProvider 还没有准备好(因为我们仍然在 $httpProvider>config 阶段).

2.将 $http 服务注入到服务初始化函数 WILL 中:

//OKangular.module('myModule').provider('myProvider', function() {//第 1 部分:初始化/配置 PROVIDER 的代码在这里(在 `config` 阶段执行)...this.$get = function($http) {//初始化/配置 SERVICE 的代码放在这里(在 `run` 阶段执行)返回我的服务;};});

由于我们现在将服务注入到服务初始化函数中,该函数在 run 阶段执行,此代码将起作用.

The main question - is it possible? I tried with no luck..

main app.js

...
var app = angular.module('myApp', ['services']);
app.config(['customProvider', function (customProvider) {

}]);
...

provider itself

var services = angular.module('services', []);
services.provider('custom', function ($http) {
});

And I've got such error:

Uncaught Error: Unknown provider: $http from services 

Any ideas?

Thanks!

解决方案

The bottom line is:

  • You CANNOT inject a service into the provider configuration section.
  • You CAN inject a service into the section which initializes the provider's service.

Details:

Angular framework has a 2 phase initialization process:

PHASE 1: Config

During the config phase all of the providers are initialized and all of the config sections are executed. The config sections may contain code which configures the provider objects and therefore they can be injected with provider objects. However, since the providers are the factories for the service objects and at this stage the providers are not fully initialized/configured -> you cannot ask the provider to create a service for you at this stage -> at the configuration stage you cannot use/inject services. When this phase is completed all of the providers are ready (no more provider configuration can be done after the configuration phase is completed).

PHASE 2: Run

During run phase all the run sections are executed. At this stage the providers are ready and can create services -> during run phase you can use/inject services.

Examples:

1. Injecting the $http service to the provider initialization function WILL NOT work

//ERRONEOUS
angular.module('myModule').provider('myProvider', function($http) {
    // SECTION 1: code to initialize/configure the PROVIDER goes here (executed during `config` phase)
    ...

    this.$get = function() {
        // code to initialize/configure the SERVICE goes here (executed during `run` stage)

        return myService;
    };
});

Since we are trying to inject the $http service into a function which is executed during the config phase we will get an error:

Uncaught Error: Unknown provider: $http from services 

What this error is actually saying is that the $httpProvider which is used to create the $http service is not ready yet (since we are still in the config phase).

2. Injecting the $http service to the service initialization function WILL work:

//OK
angular.module('myModule').provider('myProvider', function() {
    // SECTION 1: code to initialize/configure the PROVIDER goes here (executed during `config` phase)
    ...

    this.$get = function($http) {
        // code to initialize/configure the SERVICE goes here (executed during `run` stage)

        return myService;
    };
});

Since we are now injecting the service into the service initialization function, which is executed during run phase this code will work.

这篇关于在应用程序配置中的自定义提供程序中使用 $http,angular.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆