我已成功访问页面上的静态API数据。我现在正在尝试访问dynami API。我已经阅读了一些有关访问动态API的文档,但是API提供程序提供的文档与在线资源不同。我不确定要在现有代码中进行哪些更改才能访问动态API数据。这是API提供程序的链接-https://drive.google.com/file/d/0B2HH3TWuI4Fdc3RfNGVocy1pT0tuSXlLdHlPMUZSRG5GTWJV/view。我也对文档中提到的contracts
和getter
,setter
感到困惑。我应该如何以及在哪里使用它们?
这是我的代码,用于从静态json API-https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json
访问Name
,Line
和MoneyLine
数据
如何使用文档并访问实时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 <a ng-click="sort_by('awayTeamName')"><i class="icon-sort"></i></a></th>
<th class="field3">homeTeam <a ng-click="sort_by('HomeTeam')"><i class="icon-sort"></i></a></th>
--> <th class="field3">Name <a ng-click="sort_by('Name')"><i class="icon-sort"></i></a></th>
<th class="line">Line <a ng-click="sort_by('line')"><i class="icon-sort"></i></a></th>
<th class="field3">Money Line <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>
答案 0 :(得分:2)
我已根据您的要求更新了代码。我已经利用可观察对象从链接https://stackblitz.com/edit/stackoverflow-24-06-2019-ewzpst?file=src/app/app.component.html
中获取实时数据。答案 1 :(得分:0)
动态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”的字段,并使用空字符串对其进行初始化。
答案 2 :(得分:0)
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();
}
//play with `this.serverData`