本文介绍了如何在 Angular.js 选择框中设置默认选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Google 上搜索过,但找不到任何相关内容.
I have searched Google and can't find anything on this.
我有这个代码.
<select ng-model="somethingHere"
ng-options="option.value as option.name for option in options"
></select>
有一些这样的数据
options = [{
name: 'Something Cool',
value: 'something-cool-value'
}, {
name: 'Something Else',
value: 'something-else-value'
}];
输出是这样的.
<select ng-model="somethingHere"
ng-options="option.value as option.name for option in options"
class="ng-pristine ng-valid">
<option value="?" selected="selected"></option>
<option value="0">Something Cool</option>
<option value="1">Something Else</option>
</select>
如何将数据中的第一个选项设置为默认值,从而得到这样的结果.
How is it possible to set the first option in the data as the default value so you would get a result like this.
<select ng-model="somethingHere" ....>
<option value="0" selected="selected">Something Cool</option>
<option value="1">Something Else</option>
</select>
推荐答案
你可以简单地使用 ng-init 像这样
You can simply use ng-init like this
<select ng-init="somethingHere = options[0]"
ng-model="somethingHere"
ng-options="option.name for option in options">
</select>
这篇关于如何在 Angular.js 选择框中设置默认选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!