如何动态地将提供者添加到Injector? [英] How to add providers to Injector dynamically?
本文介绍了如何动态地将提供者添加到Injector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每个组件都可以使用ComponentMetadata
中的providers
属性来指定新的Provider
.
Each component can specify new Provider
s using its providers
property in ComponentMetadata
.
是否有一种方法可以从组件的constructor
中动态指定提供程序?
Is there a way to specify providers dynamically from, say, constructor
of the component?
推荐答案
我已经在引导程序部分中完成了此操作.
I've done it in the bootstrap part.
bootstrap(AppComponent,[
provide( RequestOptions, { useClass: DefaultRequestOptions } ),
provide(Http, { useFactory:
function(backend, defaultOptions) {
return new Http(backend, defaultOptions); },
deps: [XHRBackend, RequestOptions]}),
]);
我猜它也可以在组件中完成:
I'm guessing it can be done in a component too:
https://angular.io/docs/ts/Latest/api/http/Http-class.html
您可以通过向工厂函数中添加决策而不是仅仅返回相同的对象来使其动态化.
You make it dynamic by adding decisions into the factory function instead of just returning the same object.
这篇关于如何动态地将提供者添加到Injector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文