0

I have a list of objects like this :

 $scope.list =  [ { id : 1, value : "one"}, 
                  { id : 2, value : "two"},
                  { id : 3, value : "three"} ];

I want it to output a list like this :

 <select>
     <option value="1">one</option>
     <option value="2">two</option>
     <option value="3">three</option>
  </select>

What is the ng-options syntax required to do this?

Padraig
  • 3,197
  • 4
  • 18
  • 26
  • **Duplicate** http://stackoverflow.com/questions/13047923/working-with-ng-options-in-angular – Mr. Black Oct 23 '13 at 04:38
  • One More possible duplicate: http://stackoverflow.com/questions/12139152/how-to-set-value-property-in-angularjs-ng-options – Mr. Black Oct 23 '13 at 04:39

2 Answers2

1

You could try this;

<select name="yourname" ng-options="option.id as option.value for option in list"></select>
BKM
  • 6,949
  • 7
  • 30
  • 45
1

HTML

<div ng-controller = "fessCntrl"> 
 <select ng-model="selectedItem" ng-options="selectedItem as selectedItem.value for selectedItem in list" ></select>   
</div>

JS

 $scope.list =  [ { id : 1, value : "one"}, 
              { id : 2, value : "two"},
              { id : 3, value : "three"} ];
 // select 1st by default
 $scope.selectedItem = $scope.list[0];

Demo Fiddle

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225