I'm trying to set the value of 2 fields <input matInput>
and <mat-select>
programmatically. For text input everything works as expected however for the <mat-select>
on the view this field is just like it would have a value of null
. But if I would call console.log(productForm.controls['category'].value
it prints correct value that I set programmatically. Am I missing something?
Here is the code:
form config:
productForm = new FormGroup({
name: new FormControl('', [
Validators.required
]),
category: new FormControl('', [
Validators.required
]),
});
setting value:
ngOnInit() {
this.productForm.controls['name'].setValue(this.product.name);
this.productForm.controls['category'].setValue(this.product.category);
}
html:
<mat-form-field>
<mat-select [formControlName]="'category'"
[errorStateMatcher]="errorStateMatcher">
<mat-option *ngFor="let category of categories" [value]="category">
{{category.name}}
</mat-option>
</mat-select>
</mat-form-field>