Angular2选择默认的第一个选项 [英] Angular2 select default first option

查看:65
本文介绍了Angular2选择默认的第一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在默认情况下,如何在角度2的select中选择第一个选项,以下代码似乎不起作用.

How do i select the first option in a select by default in angular 2 the following code don't seems to work.

<select id="sType" class="form-control" [(ngModel)]="params.searchType"> 
     <option *ngFor="let t of sTypes" [ngValue]="t.value" [attr.selected]="$index == 0 ? true : null">{{t.name}}</option> 
</select>

推荐答案

默认情况下,您可以使用索引值选择第一个选项.

You can select first option by default using index value.

<select id="sType" class="form-control" [(ngModel)]="params.searchType"> 
 <option   *ngFor="let t of sTypes; let i = index" [attr.value]="t.value" [attr.selected]="i == 0 ? true : null">{{t.name}}</option>
</select>

这篇关于Angular2选择默认的第一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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