Skip to content

Instantly share code, notes, and snippets.

@LayZeeDK
Last active July 15, 2018 21:41
Show Gist options
  • Save LayZeeDK/1d1e8cefdb1de7bd50c96f64a25bbf92 to your computer and use it in GitHub Desktop.
Save LayZeeDK/1d1e8cefdb1de7bd50c96f64a25bbf92 to your computer and use it in GitHub Desktop.
Heroes: Mixed component model
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