X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fapp%2Fheroes%2Fheroes.component.ts;h=38ab888370747f1f27fcdad36e5b67e4141b97e2;hb=5c525c7b08b4ea57e94f1c8644297de8e443e2da;hp=1e29d4ed1da92a0054178612070458f41dfb9417;hpb=88fd95b95db3ee160af3eb441daaf69a6419c2b7;p=examples%2Fangular-tour-of-heroes diff --git a/src/app/heroes/heroes.component.ts b/src/app/heroes/heroes.component.ts index 1e29d4e..38ab888 100644 --- a/src/app/heroes/heroes.component.ts +++ b/src/app/heroes/heroes.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { Hero } from '../hero'; import { HeroService } from '../hero.service'; +import { MessageService } from '../message.service'; @Component({ selector: 'app-heroes', @@ -11,13 +12,9 @@ export class HeroesComponent implements OnInit { heroes: Hero[]; - selectedHero : Hero; - - onSelect(hero : Hero): void { - this.selectedHero = hero; - } - - constructor(private heroService : HeroService) { } + constructor( + private heroService : HeroService, + private messageService : MessageService) { } ngOnInit() { this.getHeroes(); @@ -28,4 +25,13 @@ export class HeroesComponent implements OnInit { .getHeroes() .subscribe(heroes => this.heroes = heroes); } + + add(name: string): void { + name = name.trim(); + if (!name) { return; } + this.heroService.addHero({ name } as Hero) + .subscribe(hero => { + this.heroes.push(hero); + }); + } }