I have been working on creating a form using Angular Material. I am able to successfully get and set values in text fields or text area fields, but I am having trouble setting values in a dropdown menu. My form has two JSON data sets - the first one fills the dropdown with options, and the second one is supposed to set the default value in the dropdown. However, when the page loads, the default value is not getting set.
createProductForm(): FormGroup {
return this._formBuilder.group({
CATEGORY: [this.product.categories]
});
}
ngOnInit() {
getAllCategory=[
{"TYPE_CODE": "CATEGORY","TYPE_DESC": "PUBLIC"},
{"TYPE_CODE": "CATEGORY","TYPE_DESC": "PRIVATE"},
{"TYPE_CODE": "CATEGORY","TYPE_DESC": "SYSTEM"},
]
defaultSelectCategory=[
{"CATEGORY": "PRIVATE"}
]
this.defaultCat = defaultSelectCategory[0].CATEGORY;
}
<mat-form-field appearance="outline" fxFlex="100">
<mat-label>Project</mat-label>
<mat-select formControlName="CATEGORY" [(value)]="defaultCat" required >
<mat-option *ngFor="let item of getAllCategory" value="{{item.TYPE_DESC}}" (onSelectionChange)="getCATEGORY(item)">
{{item.TYPE_DESC}}
</mat-option>
</mat-select>
<mat-icon matSuffix class="secondary-text">outlined_flag</mat-icon>
</mat-form-field>
I can't figure out why the default value is not being set for the dropdown on page load. Also, it happens to be my birthday today, so don't forget to wish me - lol!