Passing on all directives to a child element of the component

前端 未结 2 1689
耶瑟儿~
耶瑟儿~ 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<any>('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);
    }
    
    0 讨论(0)
  • 2021-02-13 02:13

    In the constructor of the directive you can do some like.

    constructor(
        @Attribute('attributeName') private attributeUno:String, 
        private element:ElementRef
    ) {
        console.log(this.attributeUno);
    }
    
    0 讨论(0)
提交回复
热议问题