import { Component, OnInit, Input } from '@angular/core';
+import { ActivatedRoute } from '@angular/router';
+import { Location } from '@angular/common';
+
+import { HeroService } from '../hero.service';
import { Hero } from '../hero';
@Component({
@Input() hero : Hero;
- constructor() { }
+ constructor(
+ private route: ActivatedRoute,
+ private heroService: HeroService,
+ private location: Location
+ ) { }
ngOnInit() {
+ this.getHero();
+ }
+
+ getHero(): void {
+ const id = +this.route.snapshot.paramMap.get('id');
+ this.heroService.getHero(id).subscribe(hero => this.hero = hero);
}
+ goBack(): void {
+ this.location.back();
+ }
}
this.messageService.add('HeroService: fetching heroes...');
return of(HEROES);
}
+
+ getHero(id: number): Observable<Hero> {
+ this.messageService.add(`HeroService: fetched hero id=${id}`);
+ return of(HEROES.find(hero => hero.id === id));
+ }
}