2

This is probably an easy answer, but I already spent days trying to figure it out, without any success.

An HTTP GET returns this body response:

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

I try to iterate trough it in Angular2:

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

How can I loop through members and read their property? The html template can do it perfectly well:

<li *ngFor="let m of session.members">
{{m.id}}
</li>
1
  • I don't really know what the problem is. Please check my answer and add a comment if you want something different. Commented Jun 12, 2016 at 14:33

2 Answers 2

3
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));
        });
    );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well, I feel stupid now. Thanks a lot Günter for your swift and clear answer. Actually I tried the forEach function in a wrong way and I abandoned it because of that.
No need to feel stupid. The brain sometimes gets a bit lost when experimenting with new stuff :D
0

If you want to iterate over it in template, try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.