I have built a search field with autocomplete. I'm listening to a observable that detects changes from a text field.
this.term.valueChanges
.debounceTime(300)
.distinctUntilChanged()
.filter(query=>query.toString().length>1)
.subscribe(query => {
//Execute search function and populate dropdown menu
})
When a user search for a string the dropdown autocomplete list is populated with suggestions. If the user then erases the search string I want the dropdown list to be removed. Is there a RxJS function that makes it possible to execute a function before subscribe if a specific condition is met on the value generated from the observable stream?
Try something like this:
this.term.valueChanges
.debounce(100)
.distinctUntilChanged()
.do(() => /* close list here */)
.where(val => val.length > 1)
.select(query => ({
query: query,
completions: completions
.filter(w => w.indexOf(query.toLowerCase()) !== -1)
}))
.subscribe(r => {
/*
* r.query contains the current search word
* r.completions contain the completions for that word
*/
});
Working basic example on JSFiddle
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.