X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fapp%2Fhero-detail%2Fhero-detail.component.ts;h=caeb7966ee0f2cc29287ac6127a9babf5bf93884;hb=3efe152e8e2be6f2dc172cf1ac476e2cb01be54b;hp=caa9c091e162601ab48c8c6d150e1aa7c2ac6aa4;hpb=aa0556604bc625981bfd2f787f7b1ef2dadb9938;p=examples%2Fangular-tour-of-heroes diff --git a/src/app/hero-detail/hero-detail.component.ts b/src/app/hero-detail/hero-detail.component.ts index caa9c09..caeb796 100644 --- a/src/app/hero-detail/hero-detail.component.ts +++ b/src/app/hero-detail/hero-detail.component.ts @@ -1,5 +1,10 @@ -import { Component, OnInit, Input } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { Location } from '@angular/common'; + +import { HeroService } from '../hero.service'; import { Hero } from '../hero'; +import { MessageService } from '../message.service'; @Component({ selector: 'app-hero-detail', @@ -8,11 +13,36 @@ import { Hero } from '../hero'; }) export class HeroDetailComponent implements OnInit { - @Input() hero : Hero; + hero: Hero | undefined; - constructor() { } + constructor( + private route: ActivatedRoute, + private heroService: HeroService, + private messageService: MessageService, + private location: Location + ) { } ngOnInit() { + this.getHero(); + } + + getHero(): void { + this.route.params.subscribe(params => { + const id: number = +params.id; + this.hero = undefined; + this.heroService.getHero(id).subscribe(hero => { + this.messageService.add('HeroDetailComponent: retrived hero ' + JSON.stringify(hero)); + this.hero = hero; + }); + }); } + save(): void { + this.heroService.updateHero(this.hero) + .subscribe(() => this.goBack()); + } + + goBack(): void { + this.location.back(); + } }