Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-options track by issue

Tags:

angularjs

I have a data set of objects that I'm displaying using ng-options. I'm binding the objects ID value to the value using track by

Currently, the data values are being included but they're being displayed with commas. For example...

$scope.items = [
   {ID: '2012', Title: 'Chicago'},
   {ID: '2013', Title: 'New York'},
   {ID: '2014', Title: 'Washington'},
];

<select ng-options="item.Title for item in items track by item.ID">
</select>

But this will render...

<option value="2,0,1,2" label="Chicago">Chicago</option>
<option value="2,0,1,3" label="New York">New York</option>

Why are these commas being added, and how can I remove them?

like image 685
Kyle Avatar asked Dec 10 '22 22:12

Kyle


1 Answers

You don't need track by:

<select ng-options="i.ID as i.Title for i in items" ng-model="someModel"></select>

After rendering you will have:

<option value="2012">Chicago</option>
<option value="2013">New York</option>
like image 159
Tomislav Avatar answered Jan 04 '23 09:01

Tomislav