Angular4 HttpClient: MySportsFeeds API Part 1

Get data from api with HttpClient module

The first thing I want to do in this app is get a list of all the pitchers in major league baseball. I used the MySportsFeeds API to find the correct endpoint to get all pitchers. I am able to use Angular’s http module to send a GET request for data using this endpoint https://api.mysportsfeeds.com/v1.1/pull/mlb/2017-regular/active_players.json?position=P found in the api’s documentation.

//app.component.ts 

import { Component, ViewChild, Inject, OnInit } from '@angular/core';
import { Http, Response, RequestOptions, Headers, Request, RequestMethod } from '@angular/http';
import 'rxjs/add/operator/map';

let headers = new Headers({ "Authorization": "Basic " + btoa('username' + ":" + 'password') });
let options = new RequestOptions({ headers: headers });
let url = 'https://api.mysportsfeeds.com/v1.1/pull/mlb/2017-regular/cumulative_player_stats.json?position=P&sort=STATS.Pitching-NP.D&limit=275';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {
   
   statData: Array<any>;

   constructor(private http: Http) {}

   loadData() {
    this.http.get(url, options)
     .map(response => response.json())
      .subscribe(res => {
        console.log(res['cumulativeplayerstats'].playerstatsentry, 'got player info res!');
        this.statData = res['cumulativeplayerstats'].playerstatsentry;
      });
   }

   ngOnInit() {
    loadData();
   }
}
//app.component.html

 <ul>
  <li *ngFor="let data of statData"> 
    {{ data.player.FirstName + ' ' + data.player.LastName + ' - ' + data.team.Abbreviation }}
  </li>
 </ul>

Check the codebase on my GitHub account!