Whenever I need to introduce a new superhero character, I will utilize
the add(string) function found in heroes/heroes.component.ts
add(name: string): void {
name = name.trim();
if (!name) { return; }
this.heroService.addHero({ name } as Hero)
.subscribe(hero => {
this.heroes.push(hero);
});
}
as well as the
addHero(Hero) function located in heroes.service.ts
addHero (hero: Hero): Observable<Hero> {
return this.http.post<Hero>(this.heroesUrl, hero, httpOptions).pipe(
tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),
catchError(this.handleError<Hero>('addHero'))
);
}
I am curious to find out where the new ID is being generated from.
Here is a working example: https://stackblitz.com/angular/ombxjmbjedp