gpt4 book ai didi

html - Angular 8 : Set default selected value for select tag

转载 作者:行者123 更新时间:2023-12-03 23:35:31 25 4
gpt4 key购买 nike

我想为选择标签设置一个默认选项值,这些值在我的组件中声明为表格并使用 ngFor 指令显示:

<form>
<div class="form-group col-4">
<span class="badge badge-theme mb-3">Quantité</span>
<select class="form-control" id="exampleFormControlSelect1">
<option *ngFor="let quantity of Quantities" [ngValue]="quantity">{{quantity}}</option>
</select>
</div>
<div class="form-group col-4">
<span class="badge badge-theme mb-3">Message personnalisé</span>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
</div>
</form>

components.ts

  Quantities = Array(50).fill(0).map((x,i)=>i);

最佳答案

你可以这样做:

component.html

 <select [(ngModel)]="seletedValue" class="form-control" id="exampleFormControlSelect1">
<option *ngFor="let quantity of Quantities" [ngValue]="quantity">{{quantity}}</option>
</select>

component.ts

seletedValue = ''; // set some default value from Quantities

component.html

<select [(ngModel)]="seletedValue" class="form-control" id="exampleFormControlSelect1">
<option value="" [disabled]="true">Select quantity</option>
<option *ngFor="let quantity of Quantities" [ngValue]="quantity">{{quantity}}</option>
</select>

关于html - Angular 8 : Set default selected value for select tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58910843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com