[英]Select default selected value in select tag in Angular 5
here is my code 这是我的代码
<select class="form-control input-transparent" id="select-field" name="address" ngModel required>
<optgroup *ngFor="let wallet of wallets" label="{{wallet.label}}">
<option value="{{ wallet.address }}" [selected]="wallet.address == wallets[0].address">{{ wallet.address }}</option>
</optgroup>
</select>
I want to select first option as selected but still on default value was selected 我想选择第一个选项,但仍选择了默认值
selected
is not supported with ngModel
selected
不与支持ngModel
You can use this sample code. 您可以使用此示例代码。 I have created a demo on stackblitz . 我已经在stackblitz上创建了一个演示。 I hope this will help/guide to you/others. 我希望这会对您/其他人有所帮助/指导。
HTML Code HTML代码
<select class="form-control input-transparent" id="select-field" name="address" required [(ngModel)]="address">
<optgroup *ngFor="let wallet of wallets;" label="{{wallet.label}}">
<option [value]="wallet.id">{{ wallet.address }}</option>
</optgroup>
</select>
.ts Code .ts代码
address:number=1;
wallets= [{
id: 1,
address: 'address1',
label: 'Label 1'
}, {
id: 2,
address: 'address 2',
label: 'Label 2'
}]
Use the <selected>
tags property value
and set it to your desired value. 使用<selected>
标签属性value
,并将其设置为所需的值。
<select value="wallets[0].address">
You can set a pre-selected option in a drop-down list by using the value attribute for the tag, ie , but it's not valid in the W3C validator. 您可以通过使用标记的value属性在下拉列表中设置预选选项,即,但是在W3C验证程序中无效。 – Apostle May 20 '15 at 15:40 How can I set the default value for an HTML <select> element? – Apostle '15 May 20 '15:40 如何设置HTML <select>元素的默认值?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.