I'm doing a term search with Angular 2. It's about the same code used in the Heroes tutorial.
My service :
search(term: string = null, page: string = null, limit: string = null): Observable<Bibliographie[]> {
let params = new URLSearchParams();
if (term) params.set('q', term);
if (page) params.set('_page', page);
if (limit) params.set('_limit', limit);
return this.http
.get('http://localhost:3000/bibliographie', {search: params})
.map(response => response.json() as Bibliographie[])
.catch(this.handleError);
}
My component:
ngOnInit(): void {
this.bibliographies = this.searchTerms
.debounceTime(300)
.switchMap(term => term
? this.bibliographieSearchService.search(term)
: this.bibliographieSearchService.search(null, "1", "20"))
.catch(error => {
console.log(error);
return Observable.of<Bibliographie[]>([]);
});
}
It works well if I type some text in the input dialog. Problem is, I want to have some results before typing anything. In the code above, I added the line
: this.bibliographieSearchService.search(null, "1", "20"))
for this purpose, hoping that a page of results would be shown when term is null. But it only works when I clear the input dialog. I also tried to load the data on init with another service function. But then, the search does not work anymore.
Any idea?
See Question&Answers more detail:os