Angular 4 select entire object

Viewed 767

I have the following:

<select [(ngModel)]="selectedMetric">
        <option *ngFor="let metric of suggestedMetrics" [value]="metric">{{metric.displayName}}</option>
</select>

Where suggestedMetrics is an array of objects (typescript class).

When I select an item from the list, the model gets updated with the string "[object Object]" instead of the object itself.

Does it only support strings? Can't I set it to an entire object? It would save me from finding it again in the original array...

1 Answers

You should use ngValue instead of value

  <option *ngFor="let metric of suggestedMetrics" [ngValue]="metric">{{metric.displayName}}</option>
Related