Answers for "mat-form field disabled"

1

mat input disabled

<mat-form-field>
    <input matInput [formControl]="inputFormControl"/>
</mat-form-field>

class YourComponent {
    inputFormControl = new FormControl({ value: null, disabled: true });
    
    this.inputFormControl.disable();
    this.inputFormControl.enable();
}
Posted by: Guest on April-08-2021
1

how to disable mat-form-field in angular 8

If you are using a FormGroup, then you should not disable the form in the HTML Template. It will not work if you try to disable in HTML together with FormControl. Instead, it should be done within the FormGroup. Try this:

  template: `
    <mat-form-field [formGroup]="form">
      <input matInput placeholder='Name' [formControlName]="formControlName">
    </mat-form-field>
  `
and:

ngOnInit() {
    this.form = this.fb.group({
        name: new FormControl({ value: '', disabled: this.disabled })
    });
}
Also...not a big deal but..you should really be doing:

public form: FormGroup;
instead of:

public form: any;
Don't forget the import as well

import { FormGroup, FormControl } from '@angular/forms';
Btw, the name inside of the form group declaration should match whatever you have set in the HTML. So:

<input formControlName="myInputName">
and

this.form = this.fb.group({
    myInputName....
});
Posted by: Guest on March-12-2021

Code answers related to "mat-form field disabled"

Browse Popular Code Answers by Language