我该怎么做ng-repeat?

时间:2013-10-23 03:28:37

标签: angularjs

我有一个像这样的对象列表:

 $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-options语法是什么?

2 个答案:

答案 0 :(得分:1)

你可以试试这个;

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

答案 1 :(得分: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