如何使用AngularJS设置select的默认选项? [英] How do I set the default option for select with AngularJS?

查看:289
本文介绍了如何使用AngularJS设置select的默认选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为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.

推荐答案

链接回答建议使用 ng-init 。我会在控制器或指令的链接中为 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设置select的默认选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆