Como definir o valor padrão para o menu suspenso PrimeNG p
Estou usando o PrimeNG no meu aplicativo angular5. Tenho um problema com o menu suspenso p
Pergunta, questão
Eu tenho p-dropdown para mostrar países. Eu vinculo as opções de seleção corretamente, pois funciona bem (esses dados são provenientes da API), mas preciso definir a opção selecionada padrão para este menu suspenso como "Índia".
Eu configurei o valor do ng-model como Índia, mas não funcionou.
meu código dummy.component.html
<div class="form-group col-md-2">
<div>
<label for="inputEmail4"> Select Country</label>
<span style="color:red">*</span>
</div>
<p-dropdown name="country" [options]="countries" [(ngModel)]="applicant.country" placeholder="select country"
(onChange)="getStatebyCountry(applicant.country,$event)" #country="ngModel" required>
</p-dropdown>
<span *ngIf="country.invalid && (country.dirty || country.touched)">
<span [hidden]="!country.hasError('required')" style="color:#ffa500">country is mandatory</span>
</span>
</div>
meu dummy.component.ts
export class dummyComponent implements OnInit {
//variable declaration scope for all controller function
applicant: any = {};
country: string; constructor() {
}
ngOnInit() {
this.applicant.country = 'India';
}
this.countries = [];
// this.countries.push({ label: 'Select Country', value: '' });
//getallcountries
this.UserService.getallcountries().subscribe(result => {
console.log("countries" + result);
this.cnt = result;
for (let i = 0; i <= this.cnt.length; i++) {
if (this.cnt[i].id === 1) {
this.countries.push({ label: this.cnt[i].name, value: this.cnt[i].id });
}
}
});