X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fapp%2Fheroes%2Fheroes.component.ts;h=38ab888370747f1f27fcdad36e5b67e4141b97e2;hb=5c525c7b08b4ea57e94f1c8644297de8e443e2da;hp=8d9f6623e37a2ac9a03670bcdf5d1c313b0f8b9a;hpb=b9527a5cd012df19c71953671e4fc4c24e91e9e4;p=examples%2Fangular-tour-of-heroes diff --git a/src/app/heroes/heroes.component.ts b/src/app/heroes/heroes.component.ts index 8d9f662..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,9 +10,28 @@ import { Component, OnInit } from '@angular/core'; }) export class HeroesComponent implements OnInit { - constructor() { } + heroes: Hero[]; + + 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); + }); + } }