Passing on all directives to a child element of the component

前端 未结 2 1695
耶瑟儿~
耶瑟儿~ 2021-02-13 02:04

I have a few custom directives which are basically designed for . And I have a custom component

There

2条回答
  •  醉话见心
    2021-02-13 02:13

    You can make every directive provide itself like it is done with ControlValueAccessor or validators

    export const MY_DIRECTIVES = new InjectionToken('MyDirectives');
    
    export const MY_DIRECTIVE1: Provider = {
      provide: MY_DIRECTIVES,
      useExisting: forwardRef(() => MyDirective1),
      multi: true
    };
    
    @Directive({
      selector: '....',
      providers: [MY_DIRECTIVE1]
    })
    class MyDirective1 {}
    

    and then in your input component

    constructor(@Optional() @Self() @Inject(MY_DIRECTIVES) private myDirectives: any[]) {
      console.log(myDirectives);
    }
    

提交回复
热议问题