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

Doesn't update selectedItem in custom dropdown

假设有这个下拉菜单:

 <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;