如何使用 AngularJS 设置选择的默认选项? [英] How do I set the default option for select with AngularJS?
本文介绍了如何使用 AngularJS 设置选择的默认选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试为 angularJS 设置默认的预选选项,但由于某种原因我不能.这是一个相关Plunkr的链接.
I have been working on trying to set the default pre-selected option for angularJS and for some reason I can't. Here is a link to the relevant Plunkr.
相关代码如下:
<select
ng-model="selectedOption"
ng-options="option.value as option.name for option in options">
</select>
我尝试过 ng-init 和许多不同的方法;我不知道如何使顶部选项成为预选的选项.
I've tried ng-init and a bunch of different ways; I can't figure how to make the top option the preselected one.
推荐答案
linked 答案建议使用 ng-init
.我会在控制器或指令的 link
函数中将默认选项分配到 selectedOption
中:
The linked answer suggests using ng-init
. I would go with assigning default option into selectedOption
in controller or directive's link
function:
$scope.selectedOption = options[0]
我认为不需要为这个简单的任务使用另一个指令.
I don't see a need to use another directive for this simple task.
这篇关于如何使用 AngularJS 设置选择的默认选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文