|
|
|
@@ -1,5 +1,8 @@ |
|
|
|
import {Component, Input, OnInit} from '@angular/core'; |
|
|
|
import { Hero } from '../hero'; |
|
|
|
import {Hero} from '../hero'; |
|
|
|
import {ActivatedRoute} from '@angular/router'; |
|
|
|
import {Location} from '@angular/common'; |
|
|
|
import {HeroService} from '../hero.service'; |
|
|
|
|
|
|
|
@Component({ |
|
|
|
selector: 'app-hero-detail', |
|
|
|
@@ -10,9 +13,27 @@ export class HeroDetailComponent implements OnInit { |
|
|
|
|
|
|
|
@Input() hero: Hero; |
|
|
|
|
|
|
|
constructor() { } |
|
|
|
constructor( |
|
|
|
private route: ActivatedRoute, |
|
|
|
private heroService: HeroService, |
|
|
|
private location: Location |
|
|
|
) { |
|
|
|
} |
|
|
|
|
|
|
|
ngOnInit() { |
|
|
|
this.getHero(); |
|
|
|
} |
|
|
|
|
|
|
|
private getHero() { |
|
|
|
// Route parameters are always strings. |
|
|
|
// The JavaScript (+) operator converts the string to a number, |
|
|
|
// which is what a hero id should be. |
|
|
|
const id = +this.route.snapshot.paramMap.get('id'); |
|
|
|
this.heroService.getHero(id) |
|
|
|
.subscribe(hero => this.hero = hero); |
|
|
|
} |
|
|
|
|
|
|
|
goBack() { |
|
|
|
this.location.back(); |
|
|
|
} |
|
|
|
} |