使用HTTP $自定义提供在内部配置的应用程序,angular.js [英] use $http inside custom provider in app config, angular.js
问题描述
主要的问题 - 这可能吗?我试过,没有运气..
The main question - is it possible? I tried with no luck..
主要app.js
...
var app = angular.module('myApp', ['services']);
app.config(['customProvider', function (customProvider) {
}]);
...
提供者本身
var services = angular.module('services', []);
services.provider('custom', function ($http) {
});
和我有这样的错误:
Uncaught Error: Unknown provider: $http from services
任何想法?
谢谢!
推荐答案
- 您的无法的注入到服务提供商的配置栏目
- 您的 CAN 的注入服务成初始化提供者的服务的栏目
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.
角框架有一个两阶段的初始化过程:
Angular framework has a 2 phase initialization process:
在配置
阶段所有的供应商都被初始化,所有的配置
部分被执行。在配置
章节中可能包含code这配置提供的对象,因此他们可以与供应商对象注入。
然而,由于供应商是厂家为服务对象,并在这个阶段的供应商并不完全初始化/配置 - > 您不能要求供应商在这个阶段为您创建一个服务 - >在配置阶段,你无法使用/注射服务。
当这个阶段完成所有的供应商都准备就绪(完成配置阶段之后没有更多的供应商的配置可以做到)。
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).
在运行
阶段所有的运行
部分被执行。在这个阶段的的供应商准备就绪,可以创建服务 - > 运行
阶段,你可以使用/注射服务中。
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.
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;
};
});
由于我们正在试图注入 $ HTTP
服务到这是在配置
阶段执行的功能,我们会得到一个错误:
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
这是什么错误实际上说的是 $ httpProvider
用来创建 $ HTTP
服务还没有准备好(因为我们仍然在配置
阶段)。
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).
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;
};
});
由于我们现在注射服务到服务的初始化函数,这是在运行
此相code将工作执行。
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屋!