一尘不染

如何在ng-options中设置默认值

angularjs

我可以在angularjs中使用默认值设置一个下拉列表,

 <select name="repeatSelect" id="repeatSelect" ng-model="repeatSelect" ng-init=" repeatSelect = data[0].id">
    <option ng-repeat="option in data" value="{{option.id}}">{{option.name}}</option>
 </select>

如何使用来达到相同的效果ng-options?我胆战心惊,

 <select name="repeatSelect" 
   id="repeatSelect" 
   ng-model="repeatSelect"
   ng-init=" repeatSelect = option.id"  
   ng-options="option.name for option in data track by option.id">
</select>

但是没用。样品小提琴在这里


阅读 295

收藏
2020-07-04

共1个答案

一尘不染

使用ng-init 为设定的默认值ng-options

这是: 演示

<select name="repeatSelect" 
   id="repeatSelect" 
   ng-model="repeatSelect"
   ng-init=" repeatSelect = data[0].id"  
   ng-options="option.id as option.name for option in data">          
</select>
2020-07-04