X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fapp%2Fheroes%2Fheroes.component.ts;h=38ab888370747f1f27fcdad36e5b67e4141b97e2;hb=HEAD;hp=b8b63147c764f4c8d775c345081e54cf56d37100;hpb=76abd89bee87b35b57e3e609038df39b8004ebea;p=examples%2Fangular-tour-of-heroes diff --git a/src/app/heroes/heroes.component.ts b/src/app/heroes/heroes.component.ts index b8b6314..38ab888 100644 --- a/src/app/heroes/heroes.component.ts +++ b/src/app/heroes/heroes.component.ts @@ -1,4 +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', @@ -7,11 +10,28 @@ import { Component, OnInit } from '@angular/core'; }) export class HeroesComponent implements OnInit { - hero = 'Windstorm'; + heroes: Hero[]; - constructor() { } + constructor( + private heroService : HeroService, + private messageService : MessageService) { } ngOnInit() { + this.getHeroes(); } + getHeroes() : void { + this.heroService + .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); + }); + } }