简体   繁体   English

Angular2 JSON返回

[英]Angular2 JSON return

I'm new to angular2. 我是angular2的新手。 I have question about get data from two API's. 我对从两个API获取数据有疑问。 From first get ID's and from another get data by ID and print it on frontend. 从第一个获取ID,然后从另一个获取ID,然后将其打印在前端。

I can't understand how to iterate JSON data from getPlayers() API 我不明白如何从getPlayers()API迭代JSON数据

Iterating is working, but it doesn't print any data. 迭代有效,但是不打印任何数据。

My service looks like this: 我的服务如下所示:

import {Injectable} from '@angular/core';
import { Http } from "@angular/http";
import "rxjs/Rx";
import {Observable} from 'rxjs/Rx';



@Injectable()
export class PlayersService {
    roster:any[];
    players:any[];

    constructor(private http: Http){
    }


   getPlayers(roster) {
    let url = "http://php.esports.cz/phone_app_roman_api/api.php/hrac?filter=id,eq,";
    return Observable.combineLatest(
      ...roster["soupiska"].map(player => this.http.get(url+ player['id_hrac'])
        .map(res => res.json()))
    ).do(players=>console.log(JSON.stringify(players)));
  }

  getRoster() {
    let url = "http://php.esports.cz/phone_app_roman_api/api.php/soupiska?filter[]=kategorie,eq,MUZ&filter[]=sezona,eq,2017&order=id_polozka&page=1,10"
    return this.http.get(url)
      .map(res => res.json())
  }


}

and my component like this: 和我的组件是这样的:

import {Component} from "@angular/core";
import {Location} from "@angular/common";
import {Page} from "../page";
import {OnInit} from '@angular/core';

import{PlayersService} from '../../services/players.service';

@Component({
    selector: 'roster-page',
    templateUrl: 'pages/roster/roster.page.html',
    styleUrls:  ['pages/roster/roster.page.css'],
    providers: [PlayersService]
})
export class RosterPage extends Page implements OnInit {

    roster:any[];
    players:any[];



    constructor(private location: Location, private playersService: PlayersService) {
        super(location);
    }

     ngOnInit() {
        this.playersService.getRoster()
        .do(roster=>this.roster=roster)
        .switchMap(roster=>this.playersService.getPlayers(roster))
        .do(players=>this.players=players)
        .subscribe();

    }



}

Template looks like this: 模板如下所示:

<StackLayout class="players">
    <StackLayout *ngFor="let player of players" orientation="horizontal" [nsRouterLink]="['/player']" pageTransition="slideTop" class="player-box">
        <Label text="{{player.hrac.jmeno}}" class="player-bane"></Label>   
    </StackLayout>
</StackLayout>   

There are many things that are wrong here: 这里有很多错误的地方:

  • First, http calls are asynchrnous, so you cannot access the value they compute until they have completed. 首先,http调用是异步的,因此您无法访问它们计算的值,直到它们完成为止。

  • If there is no .subscribe() , no Observable will do anything. 如果没有.subscribe() ,那么Observable不会做任何事情。

  • You shouldn't rely on state when dealing with observables in services, that's counter-productive. 在处理服务中的可观察变量时,您不应该依赖状态,这会适得其反。

PlayerService: PlayerService:

export class PlayersService {
  constructor(private http: Http) {}

  getPlayers(roster) {
    let url = "http://php.esports.cz/phone_app_roman_api/api.php/hrac?filter=id,eq," ;
    return Observable.combineLatest(
      ...roster[0]["soupiska"].map(player => this.http.get(url+ player['id_hrac'])
        .map(res => res.json()))
    ).do(players=>console.log(players));
  }

  getRoster() {
    let url = "http://php.esports.cz/phone_app_roman_api/api.php/soupiska?filter[]=kategorie,eq,MUZ&filter[]=sezona,eq,2017&order=id_polozka&page=1,10"
    return this.http.get(url)
      .map(res => res.json())
  }

}

RosterPage: 名册页面:

export class RosterPage extends Page implements OnInit 
{
    roster:any[];
    players:any[];

    constructor(private location: Location, private playersService: PlayersService) {
        super(location);
    }

    ngOnInit() {
        this.playersService.getRoster()
        .do(roster=>this.roster=roster)
        .switchMap(roster=>this.playersService.getPlayers(roster))
        .do(players=>this.players=players)
        .subscribe();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM