4bbfe2ee8c029183eea2fdad97edd6843ee8d459
[examples/angular-tour-of-heroes] / src / app / hero-detail / hero-detail.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { ActivatedRoute } from '@angular/router';
3 import { Location } from '@angular/common';
4
5 import { HeroService } from '../hero.service';
6 import { Hero } from '../hero';
7
8 @Component({
9   selector: 'app-hero-detail',
10   templateUrl: './hero-detail.component.html',
11   styleUrls: ['./hero-detail.component.css']
12 })
13 export class HeroDetailComponent implements OnInit {
14
15   hero: Hero | undefined;
16
17   constructor(
18     private route: ActivatedRoute,
19     private heroService: HeroService,
20     private location: Location
21   ) { }
22
23   ngOnInit() {
24     this.getHero();
25   }
26
27   getHero(): void {
28     this.route.params.subscribe(params => {
29       const id: number = +params.id;
30       this.hero = undefined;
31       this.heroService.getHero(id).subscribe(hero => this.hero = hero);
32     });
33   }
34
35   goBack(): void {
36     this.location.back();
37   }
38 }