gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:15:32 25 4
gpt4 key购买 nike

我可以在 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>

但是没有用。 sample fiddle is here

最佳答案

使用 ng-initng-options 设置默认值。

这是:demo

<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>

关于javascript - 如何在 ng-options 中设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33993468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com