How to make a formControl readonly

后端 未结 6 533
死守一世寂寞
死守一世寂寞 2020-12-15 03:24

How to make a formControl in angular readonly

I know i can do it in html like


         


        
6条回答
  •  失恋的感觉
    2020-12-15 03:27

    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.

提交回复
热议问题