简体   繁体   English

使 p 下拉菜单可编辑使其显示 [object Object]

[英]Making a p-dropdown editable makes it display [object Object]

I have an Angular web app in which I need to have an editable p-dropdown.我有一个 Angular 网络应用程序,我需要在其中有一个可编辑的 p 下拉菜单。

As soon as I changed it from this...一旦我从这个改变它......

<p-dropdown [options]="myOptionsForMyForm" optionLabel="name" appendTo="body"></p-dropdown>

to this...到这...

<p-dropdown [options]="myOptionsForMyForm" optionLabel="name" appendTo="body"   editable="true"></p-dropdown>

the p-dropdown became editable - which is good. p 下拉菜单变得可编辑 - 这很好。

HOWEVER, when the page initially loads it started displaying [object Object] rather than the value of 'name' in the drop-down.但是,当页面最初加载时,它开始在下拉列表中显示 [object Object] 而不是“name”的值。

The class that populates the p-dropdown looks like this:填充 p-dropdown 的类如下所示:

 export class MyClass {
    name: string;
    displayOrder: number;
 }

So obviously making it editable is making it display the entire MyClass object upon loading rather than just displaying the name value.因此,显然使其可编辑是使其在加载时显示整个 MyClass 对象,而不仅仅是显示名称值。

I found I could get around this by forcing it upon startup.我发现我可以通过在启动时强制它来解决这个问题。

Basically I changed this...基本上我改变了这个......

this.myForm.setValue({
  'myOption': MyClass
});

to this...到这...

this.myForm.setValue({
  'myOption': MyClass.name
});

But that messes things up elsewhere when it thinks it's getting the entire class and it's only getting a single string.但是当它认为它正在获取整个类并且它只获取一个字符串时,它会把其他地方的事情搞砸。

If it's editable, I'm basically having to choose between having the p-dropdown display correctly but behave incorrectly, or having it behave correctly but display an incorrect [object Object] value upon initial load.如果它是可编辑的,我基本上必须在 p-dropdown 显示正确但行为不正确之间做出选择,或者让它行为正确但在初始加载时显示不正确的 [object Object] 值。

Any suggestions?有什么建议?

Thanks much非常感谢

尝试添加displayProperty = "name"

<p-dropdown [options]="myOptionsForMyForm"  displayProperty = "name"  optionLabel="name" appendTo="body"   editable="true" ></p-dropdown>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM