I am trying to populate a form which has a few PrimeNg dropdowns. For simplicity let me use example similar to what is on their web site.
<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'}
];
So suppose user from table with his trips selects a row for edit. It corresponds to some complex object, which contains a City property. We need to programmatically select say Rome in that dropdown on the form.
I tried to do:
this.myFormGroup.get("selectedCity").setValue('Rome');
tried:
this.myFormGroup.get("selectedCity").patchValue('Rome');
tried adding to html:
optionLabel="name"
Nothing gets selected.
Any advice how to do it properly?
I assume I should not add
[(ngModel)]="selectedCity"
and do:
this.selectedCity='Rome';
Using Angular6 and PrimeNG 6.1.2.