hero-detail.component.ts
1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Component,Input } from '@angular/core';
import {Hero } from '../models/hero';
import { Location } from '@angular/common';
import { ActivatedRoute, Params } from '@angular/router';
//ngOnInit
import { OnInit } from '@angular/core';
import 'rxjs/add/operator/switchMap';
import { HeroService } from '../services/hero.service';
@Component({
selector: 'hero-detail',
// template: `
// <div *ngIf="hero">
// <h2>{{hero.name}} details!</h2>
// <div><label>id: </label>{{hero.id}}</div>
// <div>
// <label>name: </label>
// <input [(ngModel)]="hero.name" placeholder="name"/>
// </div>
// </div>
// `
templateUrl:'./hero-detail.component.html',
styleUrls:['./hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {
@Input() hero: Hero;
constructor(
private route: ActivatedRoute,
private location: Location,
private heroService: HeroService
) {}
ngOnInit(): void {
this.route.params
.switchMap((params: Params) => this.heroService.getHero(+params['id']))
.subscribe(hero => this.hero = hero);
}
goBack(): void {
this.location.back();
}
}