Passing on all directives to a child element of the component

烈酒焚心 提交于 2019-12-03 11:50:44

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);
}
Eliseo

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

constructor(
    @Attribute('attributeName') private attributeUno:String, 
    private element:ElementRef
) {
    console.log(this.attributeUno);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!