难以使用Angular从API获取json数据

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (46)

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

并将它们存储在我的Angular应用程序的表中。我的页面上显示的是最初的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() {
  }

}
提问于
用户回答回答于

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

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...以便一旦他们给你一些东西就结束订阅。

扫码关注云+社区

领取腾讯云代金券