使用Angular难以从API提取JSON数据

时间:2019-06-11 18:48:19

标签: json angular api

我正在尝试从API https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json提取数据

并将它们存储在我的Angular Application中的表中。前15行显示在我的页面上,而不是全部行。而且我无法获取嵌套在结果对象内部的行数据。

这是我的HTML代码


  <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>



这是我的打字稿代码

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() {
  }

}

1 个答案:

答案 0 :(得分:2)

通常,您在服务中设置实际的服务器调用(称为Model,因为它为数据建模)。这是我的一个例子。

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));
    }
    ...

在我的组件(有时是其他服务)中,我将调用API方法并以Observable形式期望结果。因此,在您的情况下,我将像这样使用我的服务:

组件

constructor(private apiService: ApiService) { }

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

嘿,记住不要调用getData1();。不止一次,否则您将有2个“热门”订阅。使用.pipe(take(1)).sub ...可以在订阅给您一些东西后终止订阅。