2017-10-01 118 views
2

在我的組件中,我從REST獲取響應,但無法在下拉列表中填充相同內容。Angular 4 populate dropdown not working

組件:

jobTitleList() { 
    this.jobList = this.getJobList().then((result) => { 
     this.jobList = result; 
     console.log('result is : ', this.jobList); 
    }); 
} 

在控制檯我得到:結果爲:["Software Developer","Support Engineer"]

HTML選擇/下拉列表:

<select (focus)="jobTitleList()"> 
    <option *ngFor="let jobTitle of jobList" value="">{{jobTitle}}</option> 
</select> 

我得到空的選擇框,我的屏幕上。請幫忙。我跟着很多帖子,但沒有幫助。提前致謝。

+0

我不確定,但檢查是否是因爲異步調用,我的意思是你的DOM正在呈現之前,我們從getJobList()服務得到響應。 –

+0

是的,我想但我無法解決它。能否請你幫忙。 – javauser

+0

在控制檯中的任何錯誤? – Alex

回答

0
job: IJob; 
jobList: IJob[]; 

jobTitleList() { 
    this.getJobList().then(result => { 
     this.jobList = result; 
    }); 
} 

<select [(ngModel)]="job" (focus)="jobTitleList()"> 
    <option *ngFor="let j of jobList" [value]="j">{{ j }}</option> 
</select>