【问题标题】:Dynamic REST API call angular动态 REST API 调用角度
【发布时间】:2019-11-09 19:18:39
【问题描述】:

我已成功访问页面上的静态 API 数据。我现在正在尝试访问 dynami API。我已经阅读了一些文档来访问动态 API,但是 API 提供者的文档与在线资源不同。我不确定必须对现有代码进行哪些更改才能访问动态 API 数据。这是来自 API 提供商的链接 - https://drive.google.com/file/d/0B2HH3TWuI4Fdc3RfNGVocy1pT0tuSXlLdHlPMUZSRG5GTWJV/view。我也对文档中提到的contractsgettersetter 感到困惑。我应该如何以及在哪里使用它们?

这是我从静态 json API 访问 NameLineMoneyLine 数据的代码 - https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json

如何使用文档并访问实时 API 数据?

api.component.ts 代码

import {Component} from '@angular/core';
import {HttpClient} from '@angular/common/http'
import {forkJoin} from 'rxjs';
import {map} from 'rxjs/operators';

@Component({
  selector: 'app-mlb-api',
  templateUrl: './mlb-api.component.html',
  styleUrls: ['./mlb-api.component.css']
})
export class MlbApiComponent  {
//allhomeTeamName;
//allawayTeamName;
allline;
allOdds;
allName;
all: Array<{line: string, name: string,oddsAmerican:string}> = [];
firstLinePerGame: Array<string>;
oddsAmericans: Array<string>;

  constructor(private http: HttpClient) {}

  ngOnInit() {

    const character = this.http.get('https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json').pipe(map((re: any) => re.events));
    const characterHomeworld = this.http.get('https://www.fantasylabs.com/api/sportevents/3/2019_06_17');

    this.firstLinePerGame = new Array<string>();
    //this.oddsAmericans = new Array<string>();

    forkJoin([character, characterHomeworld]).subscribe(([draftkingsResp, fantasylabsResp]) => {      

      //this.allhomeTeamName = draftkingsResp.map(r => r.homeTeamName);
      //this.allawayTeamName = draftkingsResp.map(r => r.awayTeamName);
      this.allName = draftkingsResp.map(r => r.name);
      this.allline = draftkingsResp.map(r=>r.offers).flat().map(r => r.outcomes).flat().map(o => o.line);
      this.allline = this.allline.filter(l => !!l);
      this.allOdds = draftkingsResp.map(r => r.offers).flat().map(r=>r.outcomes[0]).flat().map(o=>o.oddsAmerican);
      this.createAllArray();      


    });
  }

  createAllArray(): void {
    for (let i = 0; i < this.allline.length; i++) {
      let item = {
        line: this.allline[i],
        //awayTeam: this.allawayTeamName[i],
        //homeTeam: this.allhomeTeamName[i],
        name:this.allName[i],
        oddsAmerican: this.allOdds[i]
      }
      this.all.push(item);
    }
  }
}

api.component.html 代码

<table class="table table-striped table-condensed table-hover">
  <thead>
      <tr>

        <!--   <th class="awayTeamName">awayTeamName&nbsp;<a ng-click="sort_by('awayTeamName')"><i class="icon-sort"></i></a></th>
          <th class="field3">homeTeam&nbsp;<a ng-click="sort_by('HomeTeam')"><i class="icon-sort"></i></a></th>
 -->           <th class="field3">Name&nbsp;<a ng-click="sort_by('Name')"><i class="icon-sort"></i></a></th>
            <th class="line">Line&nbsp;<a ng-click="sort_by('line')"><i class="icon-sort"></i></a></th>
             <th class="field3">Money Line&nbsp;<a ng-click="sort_by('oddsAmericans')"><i class="icon-sort"></i></a></th>
      </tr>
  </thead>

  <tbody>
    <ng-container *ngFor="let item of all| paginate: { itemsPerPage: 5, currentPage: p }; let i = index">
      <tr>
        <td>{{item.name }}</td>
<!--         <td>{{item.awayTeam}}</td>
        <td>{{item.homeTeam}} </td> -->
                <td>{{item.line }}</td>

        <td>{{item.oddsAmerican}}</td>

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


<pagination-controls (pageChange)="p = $event"></pagination-controls>

【问题讨论】:

  • live api 数据是指频繁更改服务器数据吗?还是一些静态服务器数据?
  • 服务器数据频繁变化
  • 动态api是什么意思?你的意思是推送事件?我不明白意思
  • 不断变化的 API 数据,例如 2 支参加体育比赛的球队的得分

标签: angular api


【解决方案1】:

我已根据您的要求更新了代码。我已经利用 observables 在链接https://stackblitz.com/edit/stackoverflow-24-06-2019-ewzpst?file=src/app/app.component.html

中获取实时数据

【讨论】:

  • 您是否必须按照文档中的说明创建深层链接?
【解决方案2】:

可以通过以下语法生成动态 URL:

//goes right below the imports
const httpJSON = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json'})
};

//code below goes inside the component class
private getUrl = 'http://localhost:8080/arenamaster-backend/api/tournaments/profile';

constructor(private http: HttpClient) {
}

getTournament(id: string): Observable<YourObject>{
  const url = `${this.getUrl}/${id}`;
    //console.log(url);
    return this.http.get<YourObject>(url, httpJSON);
}

YourObject 应该是一个类,其字段对应于您希望接收的 JSON 数据。 IE 如果传入 JSON 数据中的字段名为“odds”并包含一个字符串,则您的类中应该有一个标记为“odds”的字段并将其初始化为空字符串。

【讨论】:

  • 我们可以在这里工作吗 - stackblitz.com/edit/stackoverflow-24-06-2019-2diykz?file=src/… 。我收到错误
  • 你在改编我的例子太字面意思了。它应该是一个关于如何构建代码的松散指南。在您的情况下,您将从 URL 或输入字段中获取 ID,然后将其提交给服务,然后将其与基本服务 URL 结合起来,然后使用它对目标服务执行 GET 请求。
  • stackblitz.com/edit/stackoverflow-24-06-2019-crdsxv 这是从我的锦标赛配置文件组件中复制的全套代码,但它不能正常工作,因为我的项目是一个全栈项目,您将缺少后端和数据库。
  • 好的,我不需要存储数据。我只需要访问实时 API 数据
【解决方案3】:

试一试:-

   import { Subscription, timer, pipe } from 'rjxs';
import { switchMap } from 'rxjs/operators';

subscription: Subscription;
statusText: string;

ngOnInit() {
    this.subscription = timer(0, 10000).pipe(
      switchMap(() => this.myservice.getdata())
    ).subscribe(result => this.serverData= result);
}

ngOnDestroy() {
    this.subscription.unsubscribe();
}

.html

//play with `this.serverData`
猜你喜欢
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 2020-04-06
  • 2017-12-26
  • 2018-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多