Permalink
Switch branches/tags
Find file Copy path
52f4c53 May 5, 2018
2 contributors

Users who have contributed to this file

@johnpapa @wardbell
64 lines (54 sloc) 1.4 KB
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Hero } from './hero';
import { HeroService } from './hero.service';
@Component({
selector: 'my-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
heroes: Hero[];
selectedHero: Hero;
addingHero = false;
error: any;
showNgFor = false;
constructor(private router: Router, private heroService: HeroService) {}
getHeroes(): void {
this.heroService
.getHeroes()
.subscribe(
heroes => (this.heroes = heroes),
error => (this.error = error)
)
}
addHero(): void {
this.addingHero = true;
this.selectedHero = null;
}
close(savedHero: Hero): void {
this.addingHero = false;
if (savedHero) {
this.getHeroes();
}
}
deleteHero(hero: Hero, event: any): void {
event.stopPropagation();
this.heroService.delete(hero).subscribe(res => {
this.heroes = this.heroes.filter(h => h !== hero);
if (this.selectedHero === hero) {
this.selectedHero = null;
}
}, error => (this.error = error));
}
ngOnInit(): void {
this.getHeroes();
}
onSelect(hero: Hero): void {
this.selectedHero = hero;
this.addingHero = false;
}
gotoDetail(): void {
this.router.navigate(['/detail', this.selectedHero.id]);
}
}