I'm trying to create a custom directive for disabling a form field but it's not working.
import { Directive, Input, ElementRef, Renderer2 } from '@angular/core';
@Directive({
selector: '[appCustomDisable]',
})
export class CustomDisableDirective {
@Input() appCustomDisable: boolean;
constructor(private el: ElementRef, private renderer: Renderer2) {}
ngOnChanges() {
if (this.appCustomDisable) {
this.renderer.setProperty(this.el.nativeElement, 'disabled',
this.appCustomDisable);
} else {
this.renderer.setProperty(this.el.nativeElement, 'disabled',
this.appCustomDisable);
}
}
}
I also tried the above using Render from '@angular/core'
this.renderer.setElementAttribute(this.el.nativeElement, 'disabled',
'true');
in app.component.html I'm using like [appCustomDisable]="myVar"
Stackblitz link https://stackblitz.com/edit/angular-lxb661