15

I'm learning Angular 2 and I've been working with static arrays, and now I'm trying to learn more about reading remote data.

My app.component.ts:

import {Component} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable} from 'rxjs/Rx';

@Component({
  selector: 'my-app',
  template:`
  <h1>Angular2 HTTP Demo App</h1>
  <h2>Foods</h2>
  <ul>
    <li *ngFor="#doc of docs">{{doc.id}}</li>
  </ul>
  `
})
export class AppComponent {

  public foods;
  public books;
  public movies;

  constructor(private http: Http) { }

  ngOnInit() {
    this.getFoods();
  }

  getFoods() {
    this.http.get('http://my API URL')
      .map((res:Response) => res.json())
      .subscribe(
        data => { this.foods = data},
        err => console.error(err),
        () => console.log('done')
      );
  }

}

This is how my API url show the results:

API URL RESuLT

  1. a json object named "docs".
  2. a json array of items with id's and other strings.

My goal is to loop each array item and show the data inside it (id, placeID, etc..)

This is my app, which makes no iteration at all:

No iteration

How I can loop with the *ngFor each of ths json array items?

TheUnreal
  • 23,434
  • 46
  • 157
  • 277

1 Answers1

11

Plunker

To loop over Array:

<li *ngFor="let doc of docs">{{doc.id}}</li>

To loop over Object Properties:

You will have to generate an Array of the object properties

generateArray(obj){
   return Object.keys(obj).map((key)=>{ return obj[key]});
}

and use it like

<li *ngFor="let doc of docs">
   <span *ngFor="let v of generateArray(doc)"> {{v}} </span>
</li>

Or you can use as Pipe.

Community
  • 1
  • 1
Ankit Singh
  • 24,525
  • 11
  • 66
  • 89
  • Thats what I have tried to do (discared the () from my code), but it's not working (no iteration at all). – TheUnreal Mar 23 '16 at 14:12
  • can you please make a live demo ? Because if data is right, Iterations must happen. there must be something wrong with how you get the data for `docs`. – Ankit Singh Mar 23 '16 at 16:04
  • What files do I actually need to upload so the app will work on my web hosting? I've been trying to upload all the directory and filese and it's about 50-100MB http://i.imgur.com/sU9INQ5.png * I Have updated my post with a running screenshot of the app* – TheUnreal Mar 23 '16 at 16:56
  • Screenshot didn't help. and you don't need to upload your app. all you need to do is create a demo with iteration code and data on [Plunker](http://plnkr.co) – Ankit Singh Mar 23 '16 at 17:02
  • see the working [plunker](http://plnkr.co/edit/RzpfJWNZ8pSML3V8RDrh?p=preview) demo I just added. – Ankit Singh Mar 23 '16 at 17:22
  • The different between your demo and my project is that in your demo the array starts and has no name, while my JSON URL is a JSON object named "docs" and inside it the array named – TheUnreal Mar 23 '16 at 18:20
  • named what ? anyway, then do this `*ngFor="#doc of docs.nameOfArray"` instead of just `docs`. – Ankit Singh Mar 23 '16 at 18:33
  • 1
    This worked for me ! I am able to iterate through a list inside another list . Thank you ! – Sourav Jul 31 '16 at 16:34
  • In the recent versions of Angular 2, the `#` sign was replaced by `let`. So looping over array would be `
  • {{doc.id}}
  • ` – David Dec 31 '16 at 20:39
  • The above solution is giving me the values which i want but *ngFor running an infinite loop. How to stop and bind the single value. – Durga Sriram Sep 20 '17 at 13:14