javascript - p-dropdown 未正确显示标签

标签 javascript angular angular5 primeng

我无法让下拉组件正常工作。下拉列表似乎检测到它应该显示的项目,因为它根据数组中的项目数量加宽项目列表。然而空格都是空白的。

enter image description here

这与 https://www.primefaces.org/primeng/#/dropdown 中的示例相同的下拉框(第一个标题为“简单”)

但是对我来说它没有显示任何内容。我复制粘贴了完全相同的代码,唯一的区别是导入。当我转到 github 存储库时,我可以看到它们导入了

import {SelectItem} from '../../../components/common/api';

import {DropdownModule} from '../../../components/dropdown/dropdown';

我使用的地方

import {SelectItem} from 'primeng/api';

import {DropdownModule} from 'primeng/dropdown';

当我尝试使用来自 github 的导入时,它说它可以在这些位置找到 dropdownmodule 和 selectitem。

这是我的代码:

interface City {

  name: string,

  code: string

}
export class Test implements OnInit {

 cities1: City[];

  selectedCity: City;
  constructor() {
    this.cities1 = [
      {label:'Select City', value:null},
      {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'}}
  ];
   }

}

这是 html

<p-dropdown [options]="cities1" [(ngModel)]="selectedCity" placeholder="Select a City" optionLabel="name" [showClear]="true"></p-dropdown>
<p>Selected City: {{selectedCity ? selectedCity.name : 'none'}}</p>

有人知道我该如何解决这个问题吗?

谢谢

最佳答案

删除optionLabel并且代码将起作用 -

<p-dropdown [options]="cities1" [(ngModel)]="selectedCity" placeholder="Select a City" [showClear]="true"></p-dropdown>

OptionLabel : Name of the label field of an option when an arbitrary objects instead of SelectItems are used as options.

关于javascript - p-dropdown 未正确显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50699282/

相关文章:

javascript - Chrome 扩展 : communication between iframe and popup

php - 即使 post 请求完成后,Firefox 仍显示等待光标和 "loading.."

javascript - Adsense/Bing Ads 可以在 Node Webkit 桌面应用程序中使用吗?

Angular 7 - 在 post Web 服务中发送 token

java - 如何在 Angular 5中未经授权使用主页中的实体数据

angular - Angular 5+ 中的服务继承

javascript - Phaser 无法检测碰撞

Angular 2 动画同时为所有元素设置动画

angular - 如何将加载的数据正确绑定(bind)到@ng-select,angular5

angular - ionic 3,延迟加载选项卡组件