//引入基本组件
import { Component,Input , OnInit} from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';

//引入 Hero 类
import { Hero } from './hero';
import { HeroService } from './hero.service';

@Component({
    moduleId:module.id,
    selector:'my-hero-detail',
    templateUrl:'hero-detail.component.html',
    styleUrls:['hero-detail.component.css']
    //providers:[HeroService]
})

export class HeroDetailComponent implements  OnInit{

    //Angular insists that we declare a target property to be an input property
    //@Input注解是做什么的？
    //该处的注解是当时做master/detail功能的时候，为了接收master传过来的参数而设置的
    //现在该页面的hero变量已经是通过服务通过id查询出来而赋值的了，所以该注解也就不是必须的
    @Input()
    hero: Hero;

    constructor(
        private heroService:HeroService,
        private route:ActivatedRoute,

    //加载该组件，就根据url中的id查询hero
    ngOnInit():void{
        this.route.params.forEach((params:Params) => {
            let id = +params['id'];
            this.heroService.getHeroById(id)
                .then(hero => this.hero = hero);
        });
    }

    //返回按钮
    goBack():void{
        this.location.back();
    }

    //保存按钮
    save():void{
        this.heroService.update(this.hero)
            .then( () => this.goBack());
    }

}
        private location:Location
    ){   }