Angular 2 dynamic dependency injection based on @Input()

前端 未结 4 1000
长发绾君心
长发绾君心 2020-12-25 13:18

Suppose I have an Angular 2 component-directive, where I want the injected dependency that the component uses to be determined by an @Input().

I want to write someth

4条回答
  •  星月不相逢
    2020-12-25 14:04

    There is a service named Inject in @angular/core module. With @Inject you can achieve alternative way of injection. But that can be done only in constructor.

    So you will need to put component's inputs in the inputs array of your @component decorator (do not use @Input decorator inside class) and then inject that input variable in constructor.

提交回复
热议问题