Created
February 12, 2018 11:14
-
-
Save petyosi/96053e393ef8a284756700ed3e59fd31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div *ngFor="let athlete of athletes"> | |
<span>{{athlete.id}}</span> | |
<span>{{athlete.name}}</span> | |
<span>{{athlete?.country?.name}}</span> | |
<span>{{athlete?.results?.length}}</span> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, OnInit} from '@angular/core'; | |
import {AthleteService} from './services/athlete.service'; | |
import {Athlete} from './model/athlete.model'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent implements OnInit { | |
private athletes: Athlete[]; | |
constructor(private athleteService: AthleteService) { | |
} | |
ngOnInit() { | |
this.athleteService.findAll().subscribe( | |
athletes => { | |
this.athletes = athletes | |
}, | |
error => { | |
console.log(error); | |
} | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Country} from './country.model'; | |
import {Result} from './result.model'; | |
export class Athlete { | |
id: number; | |
name: string; | |
country: Country; | |
results: Result[]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
import {Athlete} from '../model/athlete.model'; | |
import {Http, Response} from '@angular/http'; | |
import 'rxjs/add/operator/map' | |
import 'rxjs/add/operator/catch'; | |
import {Observable} from 'rxjs/Observable'; | |
@Injectable() | |
export class AthleteService { | |
private apiUrl = 'http://localhost:8090/athletes'; | |
constructor(private http: Http) { | |
} | |
findAll(): Observable<Athlete[]> { | |
return this.http.get(this.apiUrl) | |
.map((res: Response) => res.json()) | |
.catch((error: any) => Observable.throw(error.json().error || 'Server error')); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Country { | |
id: number; | |
name: string; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Sport} from './sport.model'; | |
export class Result { | |
id: number; | |
age: number; | |
year: number; | |
date: string; | |
gold: number; | |
silver: number; | |
bronze: number; | |
sport: Sport; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment