4

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?

Kyle
  • 1,153
  • 5
  • 28
  • 56

6 Answers6

9

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>
Tomislav
  • 3,181
  • 17
  • 20
  • 2
    I noticed that 'select as' and 'track by' do work if trak by tracks an object, while it does not work if we track an attribute (i.e. 'track by obj.value' ) – zontar Dec 01 '15 at 19:10
1

You want:

<select ng-options="obj.ID as obj.title for obj in items"></select>

Track by just helps angular internally with array sorting. See: stack overflow answer

Community
  • 1
  • 1
trees_are_great
  • 3,881
  • 3
  • 31
  • 62
1

Try this:

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

Selected ID: {{selectedItemID}}
manzapanza
  • 6,087
  • 4
  • 39
  • 48
  • This does not add the ID, it just indexes the option values (e.g. 1, 2, 3) – Kyle Jul 01 '15 at 14:13
  • It adds the ID in the value= part without commas. Several identical answers posted. You can try the same out as a plunkr in JQueryKing's answer. – trees_are_great Jul 01 '15 at 14:20
1

Your way

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

Fiddle

Alternate way

<select ng-model="selectedItem">
        <option ng-repeat="item in items" value="{{item.ID}}">{{item.Title}}</option>
</select>

Fiddle2

Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
1

function MyController($scope){
  $scope.items = [
                  {ID: '2012', Title: 'Chicago'},
                  {ID: '2013', Title: 'New York'},
                  {ID: '2014', Title: 'Washington'},
               ];
  };
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>

<body ng-controller="MyController">  
  <select ng-model="selectedItem" ng-options="item.ID as item.Title for item in items track by item.ID"></select>
  <br/>{{selectedItem}}
  </body>
  </html>

Following should work:--

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

  {{selectedItem}}
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
0

If you want it to be by id then you should use

item.id as item.name for item in items

Maverick
  • 626
  • 6
  • 11