【发布时间】:2020-06-16 04:56:31
【问题描述】:
我正在尝试填充具有一些 PrimeNg 下拉菜单的表单。 为简单起见,让我使用类似于他们网站上的示例。
<form [formGroup]="myFormGroup">
<p-dropdown [options]="cities" formControlName="selectedCity"></p-dropdown>
</form>
this.cities = [
{name: 'New York', code: 'NY'},
{name: 'Rome', code: 'RM'},
{name: 'London', code: 'LDN'},
{name: 'Istanbul', code: 'IST'},
{name: 'Paris', code: 'PRS'}
];
假设用户从表中选择了一行进行编辑。 它对应于一些复杂的对象,其中包含一个 City 属性。 我们需要在表单的下拉菜单中以编程方式选择罗马。
我尝试过:
this.myFormGroup.get("selectedCity").setValue('Rome');
试过了:
this.myFormGroup.get("selectedCity").patchValue('Rome');
尝试添加到 html:
optionLabel="name"
没有选择任何内容。
任何建议如何正确地做到这一点?
我认为我不应该添加
[(ngModel)]="selectedCity"
然后做:
this.selectedCity='Rome';
使用 Angular6 和 PrimeNG 6.1.2。
【问题讨论】:
-
因此该下拉列表中的任何值均未显示为选中状态。如何使所选值显示?但是 this.myFormGroup.get("selectedCity").value 显示罗马。注意:之后该下拉菜单应该是可编辑的。
标签: angular typescript primeng