如何在 ng-options 中设置默认值 [英] How to set default value in ng-options
本文介绍了如何在 ng-options 中设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在 angularjs 中设置一个带有默认值的下拉列表,
I can set a dropdown list with default value in angularjs as,
<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
实现相同的效果?我试过,
How can I achieve the same using ng-options
? I treid with,
<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>
但是没有用.示例 fiddle 在这里
推荐答案
使用 ng-init
设置 ng-options
的默认值.
Use ng-init
to set default value for 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>
这篇关于如何在 ng-options 中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文