2013-10-23 66 views
0

我有這樣的對象的列表:我該如何做這個ng-repeat?

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

我希望它輸出像這樣的列表:

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

所需要的NG選項語法來做到這一點?

+0

** **重複的http:// stackoverfl ow.com/questions/13047923/working-with-ng-options-in-angular –

+0

One More Possible duplicate:http://stackoverflow.com/questions/12139152/how-to-set-value-property-in-angularjs -ng選項 –

回答

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]; 

演示Fiddle

1

你可以試試這個;

<select name="yourname" ng-options="option.id as option.value for option in list"></select>