Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a text area or mat-form-field

I am using Angular (4) + Angular Material and Reactive Forms for my Form Field. How can I disable that? I tried to google for things like disabled="true" or something like that. May can you show me the right syntax? That must be easy. Thanks!

my example:

<mat-form-field class="xxx-form-full-with">     <textarea matInput placeholder="My description" formControlName="xxx"></textarea> </mat-form-field> 
like image 216
dafna Avatar asked Nov 30 '17 10:11

dafna


People also ask

How do I turn off text area?

Definition and Usage When present, it specifies that the text area should be disabled. A disabled text area is unusable and the text is not selectable (cannot be copied). The disabled attribute can be set to keep a user from using the text area until some other condition has been met (like selecting a checkbox, etc.).

How do I disable a form field?

You can disable form fields by using some CSS. To disable form fields, use the CSS pointer-events property set to “none”.


2 Answers

With reactive forms [disabled] will not work. You need to set the disabled status on the formControl instead:

this.myForm = this.formBuilder.group({   xxx: [{value: 'someValue', disabled:true}] }) 

Also remember when doing this, this field will not be included in the form object e.g when submitting. If you want to inspect all values, disabled included, use:

this.myForm.getRawValue(); 
like image 115
AT82 Avatar answered Oct 03 '22 01:10

AT82


Since you are using ReactiveForms (formControl) you should use

this.formGroupName.disable() to disable all the group form or

this.formGroupName.controls[controlNmae].disable() for a specific formControl.

PS: if you would like to enable the control again, you could use:

this.formGroupName.controls[controlNmae].enable()

like image 27
dmarquina Avatar answered Oct 03 '22 01:10

dmarquina