我使用[ngModel]
将当前选择的选项分配给下拉菜单,当页面最初加载时,这很好,但有时在页面仍然加载的情况下更新数据时,下拉菜单显示的值不正确,但模型本身仍然正确。
<select *ngIf="effect.type == ruleEffects.ApplyTemplate" class="form-select"
[disabled]="readOnly" [ngModel]="effect.impact"
(ngModelChange)="updateImpact($event, j)">
<option *ngFor="let template of templates" [value]="template.TemplateName">
{{template.TemplateName}}
</option>
</select>
验证{{effect.impact}}
表明当前选择了正确的模板,但下拉框仍会列出不正确的值。
如何解决此问题?
你能试试这样的东西吗?
<select *ngIf="effect.type == ruleEffects.ApplyTemplate" class="form-select"
[disabled]="readOnly" [(ngModel)]="effect.impact">
<option *ngFor="let template of templates" [value]="template.TemplateName">
{{template.TemplateName}}
</option>
</select>
您是否尝试使用[ngValue]而不是[value]?
<select *ngIf="effect.type == ruleEffects.ApplyTemplate" class="form-select"
[disabled]="readOnly" [(ngModel)]="effect.impact">
<option *ngFor="let template of templates" [ngValue]="template.TemplateName">
{{template.TemplateName}}
</option>
</select>