如何在Angular2中迭代包含JSON数组的API响应

时间:2016-06-12 14:29:41

标签: angular

这可能是一个简单的答案,但我已经花了好几天试图弄清楚,没有任何成功。

HTTP GET返回此正文响应:

{"members":[{"id":1484,"organization_id":3},{"id":1777,"organization_id":3},{"id":2214,"organization_id":3},{"id":2261,"organization_id":3}]}

我尝试在Angular2中迭代它:

getMembers() {
    this.http.get('http://server/user/members')
    .map(res => res.json())
    .subscribe(
        data => this.members = data.members
    );
}

如何循环访问成员并阅读其属性? html模板可以很好地完成它:

<li *ngFor="let m of session.members">
{{m.id}}
</li>

2 个答案:

答案 0 :(得分:2)

getMembers() {
    this.http.get('http://server/user/members')
    .map(res => res.json())
    .subscribe(
        data => {
            this.members = data.members;
            this.members.forEach(m => console.log(m.id));
        });
    );
}

答案 1 :(得分:0)

如果您想在模板中迭代它,请尝试:

<li *ngFor="#m of session.members | async">
   {{m.id}}
</li>