HeroDetailComponent issues a message after successfull retrival of a hero
[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 import { MessageService } from '../message.service';
8
9 @Component({
10   selector: 'app-hero-detail',
11   templateUrl: './hero-detail.component.html',
12   styleUrls: ['./hero-detail.component.css']
13 })
14 export class HeroDetailComponent implements OnInit {
15
16   hero: Hero | undefined;
17
18   constructor(
19     private route: ActivatedRoute,
20     private heroService: HeroService,
21     private messageService: MessageService,
22     private location: Location
23   ) { }
24
25   ngOnInit() {
26     this.getHero();
27   }
28
29   getHero(): void {
30     this.route.params.subscribe(params => {
31       const id: number = +params.id;
32       this.hero = undefined;
33       this.heroService.getHero(id).subscribe(hero => {
34         this.messageService.add('HeroDetailComponent: retrived hero ' + JSON.stringify(hero));
35         this.hero = hero;
36       });
37     });
38   }
39
40   goBack(): void {
41     this.location.back();
42   }
43 }