2016-06-09 13:00:26 -05:00
|
|
|
// #docplaster
|
2015-12-10 09:40:54 -08:00
|
|
|
// #docregion
|
2016-06-09 13:00:26 -05:00
|
|
|
import { Component, OnInit, OnDestroy } from '@angular/core';
|
|
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
2016-05-03 14:06:32 +02:00
|
|
|
|
2016-05-02 16:53:25 -07:00
|
|
|
import { Hero, HeroService } from './hero.service';
|
2015-12-10 09:40:54 -08:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
template: `
|
|
|
|
<h2>HEROES</h2>
|
|
|
|
<div *ngIf="hero">
|
|
|
|
<h3>"{{hero.name}}"</h3>
|
|
|
|
<div>
|
|
|
|
<label>Id: </label>{{hero.id}}</div>
|
|
|
|
<div>
|
|
|
|
<label>Name: </label>
|
|
|
|
<input [(ngModel)]="hero.name" placeholder="name"/>
|
|
|
|
</div>
|
2016-02-28 17:59:57 -08:00
|
|
|
<p>
|
|
|
|
<button (click)="gotoHeroes()">Back</button>
|
|
|
|
</p>
|
2015-12-10 09:40:54 -08:00
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
})
|
2016-06-09 13:00:26 -05:00
|
|
|
export class HeroDetailComponent implements OnInit, OnDestroy {
|
2015-12-31 16:55:53 -08:00
|
|
|
hero: Hero;
|
2015-12-10 09:40:54 -08:00
|
|
|
|
2016-06-09 13:00:26 -05:00
|
|
|
// #docregion ngOnInit
|
|
|
|
private sub: any;
|
|
|
|
|
|
|
|
// #enddocregion ngOnInit
|
2015-12-10 09:40:54 -08:00
|
|
|
// #docregion ctor
|
|
|
|
constructor(
|
2016-06-09 13:00:26 -05:00
|
|
|
private route: ActivatedRoute,
|
2016-05-02 16:53:25 -07:00
|
|
|
private router: Router,
|
|
|
|
private service: HeroService) {}
|
2015-12-10 09:40:54 -08:00
|
|
|
// #enddocregion ctor
|
|
|
|
|
2016-06-09 13:00:26 -05:00
|
|
|
// #docregion ngOnInit
|
|
|
|
ngOnInit() {
|
|
|
|
this.sub = this.route.params.subscribe(params => {
|
|
|
|
let id = +params['id']; // (+) converts string 'id' to a number
|
|
|
|
this.service.getHero(id).then(hero => this.hero = hero);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// #enddocregion ngOnInit
|
2016-05-02 16:53:25 -07:00
|
|
|
|
2016-06-09 13:00:26 -05:00
|
|
|
// #docregion ngOnDestroy
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.sub.unsubscribe();
|
2015-12-10 09:40:54 -08:00
|
|
|
}
|
2016-06-09 13:00:26 -05:00
|
|
|
// #enddocregion ngOnDestroy
|
2015-12-10 09:40:54 -08:00
|
|
|
|
2016-06-09 13:00:26 -05:00
|
|
|
// #docregion gotoHeroes-navigate
|
2015-12-10 09:40:54 -08:00
|
|
|
gotoHeroes() {
|
2015-12-31 20:04:22 -08:00
|
|
|
let heroId = this.hero ? this.hero.id : null;
|
|
|
|
// Pass along the hero id if available
|
|
|
|
// so that the HeroList component can select that hero.
|
2016-06-09 13:00:26 -05:00
|
|
|
this.router.navigate(['/heroes'], { queryParams: { id: `${heroId}`, foo: 'foo' } });
|
2015-12-10 09:40:54 -08:00
|
|
|
}
|
2016-06-09 13:00:26 -05:00
|
|
|
// #enddocregion gotoHeroes-navigate
|
2015-12-10 09:40:54 -08:00
|
|
|
}
|