Last active
November 12, 2018 21:44
-
-
Save LayZeeDK/8a9ce7c1d130e089736e19f20991bd99 to your computer and use it in GitHub Desktop.
Heroes: Container component with mutable state
This file contains hidden or 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', | |
templateUrl: './heroes.container.html', | |
}) | |
export class HeroesContainerComponent implements OnInit { | |
heroes: Hero[]; | |
constructor(private heroService: HeroService) {} | |
ngOnInit() { | |
this.getHeroes(); | |
} | |
add(name: string): void { | |
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