angular - p-dropdown does not display correct label when ngModel variable is an object
问题描述
I am using PrimeNG's p-dropdown component. When the page loads initially I am setting the ng-model
but the drop down shows first element as the selected element always.
HTML
<p-dropdown [options]="cities" [(ngModel)]="selectedCity" optionLabel="name" [showClear]="true"></p-dropdown>
<p>Selected City: {{selectedCity ? selectedCity.name : 'none'}}</p>
TS
this.cities = [
{label:'New York', value:{id:1, name: 'New York', code: 'NY'}},
{label:'Rome', value:{id:2, name: 'Rome', code: 'RM'}},
{label:'London', value:{id:3, name: 'London', code: 'LDN'}},
{label:'Istanbul', value:{id:4, name: 'Istanbul', code: 'IST'}},
{label:'Paris', value:{id:5, name: 'Paris', code: 'PRS'}}
];
Here New York is shown as the selected one in the dropdown even if I change the ng-model
to some other city.
解决方案
我认为您的问题出在optionLabel="name"
因为您的城市对象不包含名称键。
要么替换它,optionLabel="label"
要么更改你的城市对象
{label:'New York', value:{id:1, name: 'New York', code: 'NY'}}
至
{name:'New York', value:{id:1, name: 'New York', code: 'NY'}}
推荐阅读
- c++ - C++ 构造函数采用成员初始化器
- postgresql - 即使在 odoo 12 中设置 list_db = False 后导航到数据库选择器页面
- oracle - 使用 SqlDataProvider 读取大型 Oracle 表
- python - ::1/128 和 127.0.0.1/32 不是同一个版本
- drake - 错误控制想要选择小于允许的最小值的步长
- c# - 使用外部谷歌登录时,HttpContext 用户有 0 个声明
- android - 我们可以将 MDPI 设备更改为 HDPI 设备吗?
- c - 如何在 GTK+3 中处理鼠标悬停事件?
- javascript - 抓取输入值并选择要放入数组中的值,然后显示为列表
- android - Android:如何为 recyclerview 创建图例,如饼图