X-Git-Url: https://juplo.de/gitweb/?p=examples%2Fangular-tour-of-heroes;a=blobdiff_plain;f=src%2Fapp%2Fhero.service.ts;fp=src%2Fapp%2Fhero.service.ts;h=b794b8ad00b27c0a98801cee73a672aeda301a5f;hp=349d043a10ee94158ddb3260ea18c6227f8329cc;hb=b96b10a45927728a8e0c2234f494fb9eebd3df16;hpb=5c525c7b08b4ea57e94f1c8644297de8e443e2da diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts index 349d043..b794b8a 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -57,6 +57,20 @@ export class HeroService { ); } + /* GET heroes whose name contains search term */ + searchHeroes(term: string): Observable { + if (!term.trim()) { + // if not search term, return empty hero array. + return of([]); + } + return this.http.get(`${this.heroesUrl}/?name=${term}`).pipe( + tap(x => x.length ? + this.log(`found heroes matching "${term}"`) : + this.log(`no heroes matching "${term}"`)), + catchError(this.handleError('searchHeroes', [])) + ); + } + /** * Handle Http operation that failed. * Let the app continue.