angular-docs-cn/public/docs/_examples/toh-5/ts/app/hero-detail.component.ts

53 lines
1.3 KiB
TypeScript
Raw Normal View History

2015-12-23 12:42:57 -05:00
// #docplaster
// #docregion
// #docregion import-oninit, v2
2016-04-27 14:28:22 -04:00
import { Component, OnInit } from '@angular/core';
2015-12-23 12:42:57 -05:00
// #enddocregion import-oninit
// #docregion import-route-params
2016-04-27 14:28:22 -04:00
import { RouteParams } from '@angular/router-deprecated';
2015-12-23 12:42:57 -05:00
// #enddocregion import-route-params
import { Hero } from './hero';
// #docregion import-hero-service
import { HeroService } from './hero.service';
// #enddocregion import-hero-service
// #docregion extract-template
@Component({
selector: 'my-hero-detail',
// #docregion template-url
templateUrl: 'app/hero-detail.component.html',
// #enddocregion template-url, v2
styleUrls: ['app/hero-detail.component.css']
// #docregion v2
2015-12-23 12:42:57 -05:00
})
// #enddocregion extract-template
// #docregion implement
export class HeroDetailComponent implements OnInit {
// #enddocregion implement
hero: Hero;
2015-12-23 12:42:57 -05:00
// #docregion ctor
2015-12-23 12:42:57 -05:00
constructor(
private heroService: HeroService,
private routeParams: RouteParams) {
2015-12-23 12:42:57 -05:00
}
// #enddocregion ctor
2015-12-23 12:42:57 -05:00
// #docregion ng-oninit
2015-12-23 12:42:57 -05:00
ngOnInit() {
// #docregion get-id
let id = +this.routeParams.get('id');
2015-12-23 12:42:57 -05:00
// #enddocregion get-id
this.heroService.getHero(id)
2015-12-23 12:42:57 -05:00
.then(hero => this.hero = hero);
}
// #enddocregion ng-oninit
2015-12-23 12:42:57 -05:00
// #docregion go-back
2015-12-23 12:42:57 -05:00
goBack() {
window.history.back();
}
// #enddocregion go-back
}