Mike Rouse
Mike Rouse

Reputation: 1298

Why does a Select List with ng-options make the user think they've chosen a blank value?

I have a Select List that gets its values from an array.

ng-options="obj.Value as obj.Text for obj in SelectListOptions track by obj.Value"

This seems to work fine and shows the options to the user. But when the user selects one of those values the Select box just has a blank, as though they've chosen a blank value. The property is updated in the model just fine, but it gives the user an impression they've chosen a blank value.

Replicated it in this Fiddle.

Always seems to have this as the first item and this is always the selected option:

<option value="?" selected="selected"></option>

Upvotes: 0

Views: 68

Answers (1)

kasoban
kasoban

Reputation: 2167

This happens because angular uses the track by functionality to decide which item is selected, which seems to break when you use select as and track by simultaneously.

Please refer to the angular documentation for select in the section select as with trackexpr

To fix this for your case, simply remove the track by obj.Value part of your ng-options expression and it's all resolved. The only "downside" to this is that angular applies temporary tracking IDs to your select items, but usually that isn't an issue.

Upvotes: 1

Related Questions