假设有这个下拉列表:
<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];
问题是list
,loadValue
和index
是正确计算,但是 selectedItem 值没有更新,因为它向我显示了列表的第一个值,但结果不正确。