From 5c525c7b08b4ea57e94f1c8644297de8e443e2da Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 30 May 2020 09:52:02 +0200 Subject: [PATCH] 6: Get Data from a Server j) Heroes and HTTP - Add a new hero --- src/app/hero.service.ts | 8 ++++++++ src/app/heroes/heroes.component.html | 9 +++++++++ src/app/heroes/heroes.component.ts | 9 +++++++++ 3 files changed, 26 insertions(+) diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts index 6df713d..349d043 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -31,6 +31,14 @@ export class HeroService { ); } + /** POST: add a new hero to the server */ + addHero(hero: Hero): Observable { + return this.http.post(this.heroesUrl, hero, this.httpOptions).pipe( + tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)), + catchError(this.handleError('addHero')) + ); + } + /** GET hero by id. Will 404 if id not found */ getHero(id: number): Observable { this.log(`requested hero id=${id}`); diff --git a/src/app/heroes/heroes.component.html b/src/app/heroes/heroes.component.html index 2eb180e..6866fe0 100644 --- a/src/app/heroes/heroes.component.html +++ b/src/app/heroes/heroes.component.html @@ -1,4 +1,13 @@

My Heroes

+
+ + + +