简体   繁体   English

RXJS - 如果对来自observable的值满足特定条件,则执行函数

[英]RXJS - Execute function if a specific condition is met on a value from a observable

I have built a search field with autocomplete. 我已经构建了一个具有自动完成功能的搜索字段。 I'm listening to a observable that detects changes from a text field. 我正在听一个可以检测文本字段变化的observable。

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? 是否有一个RxJS函数,如果从可观察流生成的值满足特定条件,则可以在订阅之前执行函数?

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 JSFiddle运行基本示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rxjs可观察到等待,直到满足某些条件 - Rxjs observable wait until some condition is met 检查Observable的条件并执行/返回函数(angular2,rxjs) - Check condition of Observable and execute/return function (angular2, rxjs) 如何为RXJS中的每个可观察对象执行函数 - How to execute function for each observable in RXJS 仅在满足特定条件时才执行调整大小功能 - Execute resize function only if a certain condition is met 如果在 RxJS 的过滤器运算符中满足条件,如何更改变量值 - How to change variable value if condition is met in filter operator at RxJS 如何在RxJS Observable满足条件之前重复ajax请求? - How do i repeat an ajax request until a condition is met with RxJS Observable? 从对象数组中获取值作为 RxJS 可观察 - Get value from array of objects as RxJS observable RXJS6-从Promise函数返回一个Observable,该函数返回一个Observable? - RXJS6 - return an Observable from a Promise function which returns an Observable? RxJS函数从一个observable发出最后一个值,然后另一个发出true - RxJS function that emit last value from one observable then other emit true 如果不满足条件,如何在不重复的情况下在内部执行功能? - How to execute a function inside if without being repetitive if the condition is not met?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM