How to make a formControl in angular readonly
I know i can do it in html like
For the reactive-forms
, if you want to make the fields readonly, one way to do so would be to do with plain old javascript using:
(document.getElementById('abc') as HTMLInputElement).setAttribute('readonly','true');
In addition to this, you can convert the code to more typescript and angular way. Use @ViewChild
to access the element and then set the readonly
property to true:
HTML
TS
@ViewChild('element',{static:true, read: ElementRef}) element : ElementRef ;
(this.element.nativeElement as HTMLInputElement).readOnly = true;
However, you will need to check for the status of the formControl before modifying the readonly property.