You expect car.ville
to be bound to a city object, but your ngOptions
"says" the model should be bound to a city name.
If you want to display the city name in the dropdown, but associate your model (car.ville
) with the city object, change it like this:
ng-options="city as city.name for city in cities"
Additionally, in order for ngOptions
to recognize the model value it needs to reference an item in the array (and not just a different object with the same properties, since the equality check is done by reference and not by value).
See, also, this short demo.
UPDATE:
If you are sure that every item in cities
has a unique _id
, there is a simpler alternative based on the track by
feature (introduced in v1.2):
ng-options="city as city.name for city in cities track by city._id"
Now, ngOptions
can associate objects in the list with the model value not necessarily by reference, but based on the _id
property. (I.e. "same _id
" means "same object" as far as ngOptions
is concerned, so make sure your _id
s are unique.)
See, also, this other short demo.