0

I am trying to fetch data from API https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json

and store them in table in my Angular Application. Initial 15 rows are displayed on my page, not all the rows. And I cannot fetch line data which is nested inside outcome object.

Here is my HTML code


  <div class="row">

<mat-toolbar color="primary">
  <span>Welcome!</span>
</mat-toolbar>


<div class="container">
  <br>
  <h2>Information</h2>  

    <span class="right"><a href="https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json" title="sportsbook1">API endpoint</a></span>     

   <table class="table  table-hover">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>HomeTeamName</th>
        <th>AwayTeamName</th>
        <th>Start Date</th>
        <th>Offers</th>
        <th>Line</th>

        <!--<th>Image</th>-->
      </tr>
    </thead>
    <tbody>
      <ng-container *ngFor="let data of data.events">
      <tr>
        <td>{{data.id}}</td>
        <td>{{data.name }}</td>
        <td>{{data.homeTeamName }}</td>
        <td>{{data.awayTeamName}}</td>
        <td>{{data.startDate }}</td>
        <td>{{data.offers[1].label }}</td>
          <td>{{data.offers.outcomes[2].line }}        

        <!--<td><img class="image-width" src="{{contact.image}}" alt="{{contact.name}}}"></td>-->

      </tr>
    </ng-container>
    </tbody>
  </table>
</div>
</div>



Here is my typescript code

import { Component, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import { MatTableDataSource, MatSort } from '@angular/material';
import { DataSource } from '@angular/cdk/table';

@Component({
  selector: 'app-draftking',
  templateUrl: './draftking.component.html',
  styleUrls: ['./draftking.component.css']
})
export class DraftkingComponent implements OnInit {
 private apiUrl = 'https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json';
  displayedColumns = ['id','name', 'homeTeamName','awayTeamName','offers','line'];
  data: any = {};
  dataSource = this.data;

 constructor(private http: Http) {
  this.getLeague1();
  this.getData1();

 }


getData1(){
 return this.http.get(this.apiUrl)
  .map((res: Response) => res.json())
}

getLeague1() {
  this.getData1().subscribe(data => {
    console.log(data);
    this.data = data
  })
}
 ngOnInit() {
  }

}
2
  • Why you dont work with the service layer for API calls? Commented Jun 11, 2019 at 18:57
  • I am not sure how they works, could you explain a bit? Commented Jun 11, 2019 at 19:03

1 Answer 1

2

Typically you setup your actual server call in a service (called model because it models your data). Here is one of mine as an example.

service.ts

@Injectable()
export class ApiService {
    constructor(private http: HttpClient) { }

    public get<T>(path: string, routerParams?: Params): Observable<T> {
        let queryParams: Params = {};
        if (routerParams) {
            queryParams = this.setParameter(routerParams);
        }
        return this.http.get<T>(this.path(path), { params: queryParams });
    }

    public put<T>(path: string, body: Object = {}): Observable<any> {
        return this.http.put(this.path(path), body);
    }

    public post<T>(path: string, body: Object = {}): Observable<any> {
        return this.http.post(this.path(path), body);
    }

    public delete<T>(path: string): Observable<any> {
        return this.http.delete(this.path(path));
    }
    ...

In my components (sometimes other services) I will call an API method and expect results in the form of an Observable. So in your case I'd use my service like this:

component

constructor(private apiService: ApiService) { }

ngOnInit() {
    this.apiService('https://pathtoserver').subscribe(data => {
        if (data.id) {
            this.setCurrentUser();
        }
    });
}   

Hey remember to NOT call getData1(); more than once or you'll have 2 "hot" subscriptions. Use .pipe(take(1)).sub... in order to end subscriptions once they give you something.

Sign up to request clarification or add additional context in comments.

4 Comments

Should I create new component? How should and where should I make connection between service and my pages? Could you please provide more information?
So each component uses a model, view and controller file. I personally would create a service for making calls to outside servers like draftkings. Then in your component's 'constructor' you do what I show and call your service method which returns you an observable. I highly recommend Max on Udemy. He's great and the whole course is $9. udemy.com/the-complete-guide-to-angular-2
Thanks, I will definitely check it out. In ngOnInit - Do I have to use .map((res: Response) => res.json()) ?
No, new Angular versions make sure return values are Json :) good question though.

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.