javascript - Angular : Remove empty select option in angular ng-repeat

标签 javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat

我在这里使用 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;

实例: http://jsfiddle.net/choroshin/5YDJW/7/

关于javascript - Angular : Remove empty select option in angular ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21361214/

相关文章:

javascript - 如何在javascript中使用UTF8

angularjs - 获取输入的名称 angularjs

javascript - 自动建议 使用 ng-model 和过滤器使用多个特定对象属性搜索结果

javascript - 如何将 AngularJS 指令应用于 ajax 内容?

javascript - 如何将值传递给扩展窗口的文本字段? (Firefox 网络扩展)

javascript - 为什么 Mongoose 的 .save 不能在 ReactJS 中工作?

angularjs - 在 Angularjs 指令隔离范围内定义函数

angularjs - 无法弄清楚如何使用 $parsers 和 $formatters 创建指令

PHP日期到Javascript日期变成时差?

javascript - 在 AngularJS 中为列表项加载部分 *没有* 路由