Last active
July 15, 2018 21:41
-
-
Save LayZeeDK/1d1e8cefdb1de7bd50c96f64a25bbf92 to your computer and use it in GitHub Desktop.
Heroes: Mixed component model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { Hero } from '../hero'; | |
import { HeroService } from '../hero.service'; | |
@Component({ | |
selector: 'app-heroes', | |
styleUrls: ['./heroes.component.css'], | |
templateUrl: './heroes.component.html', | |
}) | |
export class HeroesComponent implements OnInit { | |
heroes: Hero[]; | |
constructor(private heroService: HeroService) {} | |
ngOnInit() { | |
this.getHeroes(); | |
} | |
add(name: string): void { | |
name = name.trim(); | |
if (!name) { return; } | |
this.heroService.addHero({ name } as Hero) | |
.subscribe(hero => { | |
this.heroes.push(hero); | |
}); | |
} | |
delete(hero: Hero): void { | |
this.heroes = this.heroes.filter(h => h !== hero); | |
this.heroService.deleteHero(hero).subscribe(); | |
} | |
getHeroes(): void { | |
this.heroService.getHeroes() | |
.subscribe(heroes => this.heroes = heroes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment