如何在Angular 6中动态选择option
?该页面上有许多select
,如下所示:
如果选项的值等于parcela.forma_parcela
,如何动态选择?
parcela.forma_parcela = [0,1,2,3,4,5,6];
<select (change)="setBancoParcela($event.target.value, parcela)" class="input-default" name="forma_pagamento_1" id="forma_pagamento_1">
<option value="0" [selected]="parcela.forma_parcela == this.value">Banco</option>
<option value="1" [selected]="parcela.forma_parcela == this.value">BNDES</option>
<option value="2" [selected]="parcela.forma_parcela == this.value">Boleto</option>
<option value="3" [selected]="parcela.forma_parcela == this.value">Cartão de Crédito</option>
<option value="4" [selected]="parcela.forma_parcela == this.value">Cartão de Débito</option>
<option value="5" [selected]="parcela.forma_parcela == this.value">CH Descontado</option>
<option value="6" [selected]="parcela.forma_parcela == this.value">Cheque</option>
<option value="7" [selected]="parcela.forma_parcela == this.value">DDA</option>
<option value="8" [selected]="parcela.forma_parcela == this.value">Débito Automático</option>
<option value="9" [selected]="parcela.forma_parcela == this.value">Depósito em C/C</option>
</select>
答案 0 :(得分:2)
使动态变化的正确方法是:
<select id="select-type-basic" [(ngModel)]="status">
<option *ngFor="let status_item of status_values">
{{status_item}}
</option>
</select>
值应避免在选项内部使用,因为这将设置“选择字段”的默认值。默认选择应该与[(ngModel)]绑定,并且应该同样声明选项。
status : any = "Completed";
status_values: any = ["In Progress", "Completed", "Closed"];
答案 1 :(得分:0)
您可以使用ngModel指令,并从类中导出选项。
答案 2 :(得分:0)
我用<option *ngFor="let forma of formas_pagamentos" value="{{forma.value}}" [selected]="forma.value == parcela.forma_parcela">{{forma.nome}}</option>
答案 3 :(得分:0)
尝试
<select
class="custom-select"
name="ratings"
[ngModel]="ratings.OverallRating">
<option
*ngFor="let orc of ratings?.RatingChoices"
[value]="orc.ratingChoice">
{{orc.ratingDescription}}
</option>
</select>