I need some help understanding how to use the HTML datalist
with Angular. I have an object here. I would like the dropdown to display the make and model
of the cars. But when you select a car
, the selectedCar
variable should be the entire car object. But the input
should still only show the make and model
.
cars = [{
make: 'Ford',
model: 'GTX',
color: 'green'
}, {
make: 'Ferarri',
model: 'Enzo',
color: 'red'
}, {
make: 'VW',
model: 'Amarok',
color: 'white'
}]
...
<input list="id-car" [(ngModel)]="selectedCar">
<datalist id="id-car">
<option *ngFor="let car of cars" [value]="car">{{car.make}} - {{car.model}}</option>
</datalist>
Here is a place to play with this: https://stackblitz.com/edit/angular-cwmwke