I'm building a custom directive where I want to read one of the attributes (formControlName) of the native element, and then conditionally add one or more attributes to the element.
However, when I console.log the native element's attribute, I get:
undefined
Here's what I tried:
@Directive({
selector: '[appInputMod]'
})
export class InputModDirective implements OnInit {
constructor(public renderer: Renderer2, public hostElement: ElementRef) { }
@Input()
appInputMod() { }
ngOnInit() {
console.log(this.hostElement.nativeElement.formcontrolname);
const el = this.hostElement.nativeElement;
if (el.formcontrolname === 'firstName')
{
this.renderer.setAttribute(this.hostElement.nativeElement, 'maxlength', '35');
}
}
}
How can I read this attribute name from within the directive?
ngAfterViewInitinstead ofngOnInit