2015-12-10 09:40:54 -08:00
|
|
|
// #docregion
|
2016-05-02 16:53:25 -07:00
|
|
|
import { Component } from '@angular/core';
|
2016-05-03 14:06:32 +02:00
|
|
|
import { OnActivate, Router, RouteSegment } from '@angular/router';
|
|
|
|
|
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-08 01:06:25 +02:00
|
|
|
export class HeroDetailComponent implements OnActivate {
|
2015-12-31 16:55:53 -08:00
|
|
|
hero: Hero;
|
2015-12-10 09:40:54 -08:00
|
|
|
|
|
|
|
// #docregion ctor
|
|
|
|
constructor(
|
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-05-02 16:53:25 -07:00
|
|
|
|
|
|
|
// #docregion OnActivate
|
|
|
|
routerOnActivate(curr: RouteSegment): void {
|
|
|
|
let id = +curr.getParam('id');
|
|
|
|
this.service.getHero(id).then(hero => this.hero = hero);
|
2015-12-10 09:40:54 -08:00
|
|
|
}
|
2016-05-02 16:53:25 -07:00
|
|
|
// #enddocregion OnActivate
|
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.
|
|
|
|
// Add a totally useless `foo` parameter for kicks.
|
|
|
|
// #docregion gotoHeroes-navigate
|
2016-05-02 16:53:25 -07:00
|
|
|
this.router.navigate([`/heroes`, {id: heroId, foo: 'foo'}]);
|
2015-12-31 20:04:22 -08:00
|
|
|
// #enddocregion gotoHeroes-navigate
|
2015-12-10 09:40:54 -08:00
|
|
|
}
|
|
|
|
}
|