When using the Angular Material select dropdown API, only a value is emitted. Both [(ngModel)]
and (selectionChange)
will only emit a single member, not the entire object's data fields.
For example, it will only emit something like food.value = 2, without emitting other class fields such as foodName, foodDescription, foodWeight, etc. Is there a way to emit the entire object for food, based on its value? This solution should work for any Material Dropdown in the future, not just specific cases. Are there specific options provided by Angular Material that allow for this?
The answers provided below seem to only address a specific scenario, but I am looking for a more universal solution.
Link to Angular Material Select API
<mat-form-field>
<mat-label>Favorite food</mat-label>
<mat-select>
<mat-option *ngFor="let food of foods" [value]="food.value">
{{food.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
As an example, for value 2, we would need fields like foodCategory, Calories, and others from the corresponding class.
export class SelectOverviewExample {
foods: Food[] = [
{value: '0', viewValue: 'Steak', foodCategory: 'Meat', Calories: 50},
{value: '1', viewValue: 'Pizza', foodCategory: 'Carbs', Calories: 100},
{value: '2', viewValue: 'Apple', foodCategory: 'Fruit', Calories: 25}
];
}
Is there a better solution available? If not, I may need to implement this within the selectionChange
event handler. It seems like Angular Materials should provide a more straightforward option.
this.foods.find(x=>x.value =="2")
Please note that any proposed solutions should be able to handle different classes with various members.