【问题标题】:Angular 4 populate dropdown not workingAngular 4填充下拉菜单不起作用
【发布时间】:2017-10-01 04:51:07
【问题描述】:

在我的组件中,我从 REST 获得响应,但无法在下拉列表中填充相同的响应。

组件:

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>

我的屏幕上出现空的选择框。请帮忙。我关注了很多帖子,但没有帮助。提前致谢。

【问题讨论】:

  • 我不确定,但请检查是否是因为异步调用,我的意思是在我们从 getJobList() 服务获得响应之前,您的 DOM 正在呈现。
  • 是的,但我无法解决。你能帮忙吗?
  • 控制台有错误吗?
  • 在 ngOnInit() 函数中调用你的函数 jobTitleList()。看看有没有帮助。

标签: angular typescript angular4-forms


【解决方案1】:
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>

【讨论】:

    猜你喜欢
    • 2013-11-27
    • 2012-07-10
    • 2019-03-09
    • 2018-02-12
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    相关资源
    最近更新 更多