what learned angular 2 docs how make live search using 1 stream of terms (a search 1 parameter).
private searchtermstream = new subject<string>(); search(term: string) { this.searchtermstream.next(term); } items: observable<string[]> = this.searchtermstream .debouncetime(300) .distinctuntilchanged() .switchmap((term: string) => this.wikipediaservice.search(term));
what need making live search restaurants 2 parameters (i have created rest webservice accepting 2 parameters: term , region, , returning list of available restaurants).
use combinelatest()
create observable... combining latest value of 2 original observables.
private termobservable = new subject<string>(); private regionobservable = new subject<string>(); private criteriaobservable = observable.combinelatest(this.termobservable, this.regionobservable, (term, region) => {term, region});
then, you're doing in question, can use criteriaobservable
implement search. function passed switchmap()
take object containing latest entered term , latest entered region.
make sure pass comparison function distinctuntilchanged()
, because uses ===
compare values default, , rather want tuples of term/region equal when both terms , regions equal:
items: observable<string[]> = this.criteriaobservable .debouncetime(300) .distinctuntilchanged((o1, o2) => o1.term === o2.term && o1.region === o2.region) .switchmap(criteria => this.restaurantservice.search(criteria.term, criteria.region));
Comments
Post a Comment