How to add providers to Injector dynamically?

后端 未结 4 1118
北海茫月
北海茫月 2021-01-01 20:42

Each component can specify new Providers using its providers property in ComponentMetadata.

Is there a way to specify provider

4条回答
  •  孤城傲影
    2021-01-01 21:31

    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.

提交回复
热议问题