I am creating a reactive form with mat-select within.
<mat-form-field>
<mat-select placeholder="Salutation*" formControlName="salutation">
<mat-option *ngFor="let salutation of salutations" [value]="salutation.id">
{{ salutation.label }}
</mat-option>
</mat-select>
</mat-form-field>
Form:
this.form = this.fb.group({
person: this.fb.group({
salutation: ['', Validators.required],
firstName: ['', Validators.required],
lastName: ['', Validators.required],
email: ['', Validators.required]
})
});
I need to disable/enable this select depending on other inputs value. Since [disabled] is no longer supported by reactive forms I used the following
this.form.get('person').get('salutation').disable();
the issue is when I try to enable the select back using
this.form.get('person').get('salutation').enable();
It just does not work. Any ideas why?
PS: using the disabled property works fine but it throws a warning
It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true when you set up this control in your component class, the disabled attribute will actually be set in the DOM for you. We recommend using this approach to avoid 'changed after checked' errors.
Example:
form = new FormGroup({
first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
last: new FormControl('Drew', Validators.required)
});
Thanks!
this.form.controls['saluation'].disabled = true;- Deepak Kumar T P