Add search to the dashboard
This commit is contained in:
parent
5f12d19dbc
commit
64c40998b9
|
@ -5,3 +5,5 @@
|
|||
{{hero.name}}
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<app-hero-search></app-hero-search>
|
|
@ -66,6 +66,21 @@ export class HeroService {
|
|||
);
|
||||
}
|
||||
|
||||
/* GET heroes whose name contains search term */
|
||||
searchHeroes(term: string): Observable<Hero[]> {
|
||||
if (!term.trim()) {
|
||||
// if not search term, return empty hero array.
|
||||
return of([]);
|
||||
}
|
||||
return this.http.get<Hero[]>(`${this.heroesUrl}/?name=${term}`)
|
||||
.pipe(
|
||||
tap(x => x.length ?
|
||||
this.log(`found heroes matching "${term}"`) :
|
||||
this.log(`no heroes matching "${term}"`)),
|
||||
catchError(this.handleError<Hero[]>('searchHeroes', []))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Http operation that failed.
|
||||
* Let the app continue.
|
||||
|
|
Loading…
Reference in New Issue