5: Add In-app Navigation
[examples/angular-tour-of-heroes] / src / app / hero-detail / hero-detail.component.ts
1 import { Component, OnInit, Input } 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   @Input() hero : Hero;
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     const id = +this.route.snapshot.paramMap.get('id');
29     this.heroService.getHero(id).subscribe(hero => this.hero = hero);
30   }
31
32   goBack(): void {
33     this.location.back();
34   }
35 }