When working with HTML, I encountered an issue where the ngModel
was not displaying even though the selectedValueModel was already assigned. I also tried using [ngValue]
, which resulted in the value being passed as undefined
to my ngModelChange
function.
Any help would be greatly appreciated.
foods: Food[] = [
{value: 'steak-0', viewValue: 'Steak'},
{value: 'pizza-1', viewValue: 'Pizza'},
{value: 'tacos-2', viewValue: 'Tacos'}
];
selectedValueModel = {value: 'steak-0', viewValue: 'Steak'};
@Output() selectedValueChange = new EventEmitter()
changing(newValue: any) {
this.selectedValueModel = newValue
this.selectedValueChange.emit(newValue)
}
Here is my HTML:
<mat-form-field>
<mat-select placeholder="Favorite food"
[(ngModel)]="selectedValueModel.value"
(ngModelChange)="changing($event)" name="food">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{food.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>