$对外提供配置块 [英] $provide outside config blocks
问题描述
我当然缺少有关喷油器的一些基本观点,但我不明白为什么正是这种
I'm certainly missing some fundamental point about the injector, but I fail to understand why exactly this
angular.module('app').config(function ($provide) {
...
});
这
angular.module('app').config(function ($injector) {
$injector.invoke(function ($provide) { ... });
});
工作按预期,而这种
app.run(function($provide) {
...
});
将抛出
错误:[$喷油器:unpr]未知提供商:$ provideProvider< - $提供
Error: [$injector:unpr] Unknown provider: $provideProvider <- $provide
从上面所示,配置
与供应商的一些特殊关系,而运行
涉及的情况下,还我不确定,使得配置
块,这样特殊的东西。
As follows from the above, config
has some special relationship with providers, while run
deals with instances, yet I'm unsure about the thing that makes config
blocks so special.
作为一个后果,就是有没有办法去 $提供
之外配置
块,例如与 angular.injector()
(尽管它似乎得到供应商的实例也)?
As a consequence of that, is there no way to get to $provide
outside config
blocks, e.g. with angular.injector()
(though it seems that it gets provider instances also)?
问题,除了单纯的好奇心,也有一些现实的考虑。在1.4的所有 $提供
功能被暴露到模块,但事实并非如此1.3。
The question, besides mere curiosity, also has some practical considerations. In 1.4 all of $provide
functions are exposed to module, but that's not true for 1.3.
推荐答案
一些角喷油器的研究,我能够给一个详尽的答案,我自己的问题后。
After some Angular injector study I was able to give an exhaustive answer to my own question.
从本质上讲, $注射器
的在配置
块和提供商
构造函数和 $注射器
的是两个不同的服务具有相同的名称,这是明确的内部供应商/实例缓存定义, $提供
(此一并>一个是服务者缓存被定义,因此它可以在配置
被注入专用)。
Essentially, $injector
in config
blocks and provider
constructor functions and $injector
everywhere else are two different services with the same name, which are defined on internal provider/instance cache explicitly, together with $provide
(this one is being defined in provider cache, hence it can be injected in config
only).
虽然一般不推荐,因为可能的竞争条件,就可以到实例缓存暴露内部服务,并配置专用 $提供
和 $注射器
可用于注射后配置阶段已经结束:
While generally not recommended because of probable race conditions, it is possible to expose internal services to instance cache and make config-specific $provide
and $injector
available for injection after config phase has ended:
app.config(function ($provide, $injector) {
$provide.value('$providerInjector', $injector);
$provide.value('$provide', $provide);
});
可能的应用程序配置服务提供商的任何时间(如果可能)
The possible applications are configuring service providers any time (if possible)
app.run(function ($providerInjector) {
var $compileProvider = $providerInjector.get('$compileProvider');
...
});
和在运行时定义新的组件
and defining new components at run-time
app.run(function ($provide) {
$provide.controller(...);
...
});
这篇关于$对外提供配置块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!