How to set default value for PrimeNG p-dropdown

后端 未结 8 928
南笙
南笙 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:51

    Update in PrimgNg

    While setting default value for dropdown in Primeng need to be little careful.

    <p-dropdown name="country" [options]="countries" [(ngModel)]="country" placeholder="select country" (onChange)="changeCountry()"></p-dropdown>
    

    country should be a number not string.

    You can typecast it, if it is a string.

    country: Number("1");
    
    0 讨论(0)
  • 2020-12-09 18:54

    Try to replace

    this.applicant.country = 'India';
    

    with

    this.applicant = {country: 'India'};
    

    Edit

    Display your p-dropdown once you got the data from your API.

    <div *ngIf="dataLoaded">
      <p-dropdown [options]="countries" [(ngModel)]="applicant.country"></p-dropdown>
    </div>
    

    See Plunker

    0 讨论(0)
提交回复
热议问题