How to set default value for PrimeNG p-dropdown

后端 未结 8 1089
南笙
南笙 2020-12-09 17:53

I am using PrimeNG in my angular5 app. I have issue with p-dropdown

Question

I have p-dropdown for showing countries. I bind the select opti

8条回答
  •  春和景丽
    2020-12-09 18:34

    My solution was to have the countries loaded in the controller before setting the form field (ngModel or formControl). Also keep the same type of the key. Don't use number for the form control and string for the list:

    // first get the cities from the server
    response.cities.forEach(city => {
                          this.dropdowns['cities'].push({ value: city.id, label: element.city }); });
    
    // when setting the form
    city_id: new FormControl(this.user.city_id)
    

    In the code above this.user.city_id and city.id has the same type number

提交回复
热议问题