72 lines
1.7 KiB
TypeScript
72 lines
1.7 KiB
TypeScript
// #docregion
|
|
import { Component, OnInit } from '@angular/core';
|
|
import { Router } from '@angular/router';
|
|
|
|
import { Hero } from './hero';
|
|
import { HeroService } from './hero.service';
|
|
// #docregion hero-detail-component
|
|
|
|
@Component({
|
|
selector: 'my-heroes',
|
|
templateUrl: 'app/heroes.component.html',
|
|
styleUrls: ['app/heroes.component.css']
|
|
})
|
|
// #enddocregion hero-detail-component
|
|
export class HeroesComponent implements OnInit {
|
|
heroes: Hero[];
|
|
selectedHero: Hero;
|
|
addingHero = false;
|
|
// #docregion error
|
|
error: any;
|
|
// #enddocregion error
|
|
|
|
constructor(
|
|
private router: Router,
|
|
private heroService: HeroService) { }
|
|
|
|
getHeroes(): void {
|
|
this.heroService
|
|
.getHeroes()
|
|
.then(heroes => this.heroes = heroes)
|
|
.catch(error => this.error = error);
|
|
}
|
|
|
|
// #docregion addHero
|
|
addHero(): void {
|
|
this.addingHero = true;
|
|
this.selectedHero = null;
|
|
}
|
|
|
|
close(savedHero: Hero): void {
|
|
this.addingHero = false;
|
|
if (savedHero) { this.getHeroes(); }
|
|
}
|
|
// #enddocregion addHero
|
|
|
|
// #docregion deleteHero
|
|
deleteHero(hero: Hero, event: any): void {
|
|
event.stopPropagation();
|
|
this.heroService
|
|
.delete(hero)
|
|
.then(res => {
|
|
this.heroes = this.heroes.filter(h => h !== hero);
|
|
if (this.selectedHero === hero) { this.selectedHero = null; }
|
|
})
|
|
.catch(error => this.error = error);
|
|
}
|
|
// #enddocregion deleteHero
|
|
|
|
ngOnInit(): void {
|
|
this.getHeroes();
|
|
}
|
|
|
|
onSelect(hero: Hero): void {
|
|
this.selectedHero = hero;
|
|
this.addingHero = false;
|
|
}
|
|
|
|
gotoDetail(): void {
|
|
this.router.navigate(['/detail', this.selectedHero.id]);
|
|
}
|
|
}
|