我在这里使用 ng-repeat 来呈现具有不同值和文本的选项,并且还设置了默认选项。但 angular 再次添加了一个空的未定义选项。
<select ng-model="newItem.financial_year_id" required style="min-width:180px;">
<option ng-repeat="financial in account_year" value="{{financial.id}}" ng-selected="financial.status=='Active'">{{financial.financial_year}}</option>
</select>
现在事件选项已被选中,但空选项仍在呈现。
最佳答案
当传递给 ng-options 的一组选项中不存在 ng-model 引用的值时,会生成空选项。
您可以在 stackoverflow 上找到完整的答案和示例。这是 link
更新:
举个例子:
<select ng-model="newItem.financial_year_id" required style="min-width:180px;">
<option ng-repeat="financial in account_year" value="{{financial.id}}" ng-selected="financial.status=='Active'">{{financial.financial_year}}</option>
</select>
在 Controller 中:
$scope.newItem={};
$scope.account_year=[{id:1,financial_year:"2013-2014",status:"Active"},
{id:2,financial_year:"2014-2015",status:"Inactive"}]
//remove empty option
$scope.newItem.financial_year_id=$scope.account_year[0].id;
关于javascript - Angular : Remove empty select option in angular ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21361214/