I'm struggling to retrieve an array from a JSON API and then loop through it. I can't seem to grasp how it all fits together. Any guidance would be greatly appreciated.
This is what my service looks like:
import {Injectable} from '@angular/core';
import { Http } from "@angular/http";
import "rxjs/Rx";
@Injectable()
export class PlayersService {
roster:Roster[];
constructor(private http: Http){
this.roster = [];
}
getPlayer(id) {
for (let player of this.roster) {
console.log(player["id"]);
}
}
getRoster(season,category) {
this.roster.push(this.http.get("http://API JSON LIST OF ID")
.map(res => res.json()));
}
}
interface Roster {
id:number
}
This is how I invoke it:
ngOnInit() {
this.getRoster();
this.getPlayers();
}
Can someone point out where I'm going wrong?