不更新自定义下拉列表中的选定项



假设有这个下拉列表:

<p-dropdown [options]="list" [(ngModel)]="code">
<ng-template let-item pTemplate="selectedItem">
{{ item.value }} - {{ item.label }}
</ng-template>
<ng-template let-item pTemplate="item">
{{ item.value }} - {{ item.label }}
</ng-template>
</p-dropdown>

在我的 ts 中,我有:

//loadValue in an object that I have just loaded with this attribute({label, value]}
//list is a list with current dropdown list and it is in this way ({label,vale}]
let index= this.list.findIndex(x => x['value'] === this.loadValue['value']);
this.code= this.list[index];

问题是listloadValueindex是正确的计算,但 selectedItem 值没有更新,因为它向我显示了列表的第一个值,但它的结果不正确。

您将项目分配给 ngModel 而不是值。 您需要传递值。这显然是一个问题,但是如果您认为它在分配后没有更新,则需要调试实际行为。

this.code= this.list[index].value;

最新更新