AngularJS select 在 ng-repeat 中

AngularJS select in ng-repeat

我正在用 ng-repeat 创建一个 <select> 的列表,每个 select 中有一个 item.quantity 我需要 selected ] 在列表中,我尝试了几种方法都没有成功。任何帮助,将不胜感激。

<div ng-repeat="item in items">

   <select ng-model="selCartQuantity">
     <option index="1" ng-selected="item.quantity=='1'">1</option>
     <option index="2" ng-selected="item.quantity=='2'">2</option>
     <option index="3" ng-selected="item.quantity=='3'">3</option>
     <option index="4" ng-selected="item.quantity=='4'">4</option>
     <option index="5" ng-selected="item.quantity=='5'">5</option>
   </select>

</div>

在您的控制器中放入以下代码(当然您将 $scope.items 更改为您的实际对象)

$scope.items = [{name:'aaaaa'},{name:'bbbbb'}];
$scope.options= [1,2,3,4,5];
$scope.items.forEach(function(item,index){
  
  item.selCartQuantity= $scope.options[0];
  
 });


and in the html markup you put the following 
<div ng-repeat="item in items">

 <select ng-model="item.selCartQuantity" ng-options="option for option in options"></select>

</div>

`


我建议查看 angular select driective,因为您根本不必执行 ng-repeat。 select 指令会自动将 selected 选项设置为模型中的选项。