How to access the Component on a Angular2 Directive?

前端 未结 5 744
無奈伤痛
無奈伤痛 2020-12-23 22:48

I\'m doing some tests with Angular 2 and I have a directive (layout-item) that can be applied to all my components.

Inside that directive I want to be able to read s

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 23:22

    It seems that most convenient and clean way is to use provider alias:

    //ParentComponent declaration
    providers: [{ provide: Parent, useExisting: forwardRef(() => ParentComponent) }]
    

    where Parent is separate class that works as OpaqueToken and abstract class at the same type.

    //directive
    constructor(@Optional() @Host() parent:Parent) {}
    

    Each component that is accessed from child directive should provide itself.

    This is described in documentation: link

提交回复
热议问题