rxjs angular 2:具有多个术语的搜索筛选器

rxjs angular 2:具有多个术语的搜索筛选器,angular,rxjs,angular2-services,rxjs5,Angular,Rxjs,Angular2 Services,Rxjs5,我正在尝试使用多个术语实现搜索。N 我读了这篇博客:类似的情况。但我不明白如何用动态观察(搜索词输入)实现它。 假设我有30个搜索词的过滤器。简单的解决方案是对所有搜索词组件使用CombineTest,但效率不高。 我的目标是订阅第一次变化时的观测值 我尝试将CombineTest运算符与动态数组一起使用,但没有成功:( 我的代码: import{Component,OnInit,Input,ViewChildren,QueryList,AfterViewInit}来自'@angular/co

我正在尝试使用多个术语实现搜索。N

我读了这篇博客:类似的情况。但我不明白如何用动态观察(搜索词输入)实现它。 假设我有30个搜索词的过滤器。简单的解决方案是对所有搜索词组件使用CombineTest,但效率不高。 我的目标是订阅第一次变化时的观测值

我尝试将CombineTest运算符与动态数组一起使用,但没有成功:(

我的代码:

import{Component,OnInit,Input,ViewChildren,QueryList,AfterViewInit}来自'@angular/core';
从“../../models/Table”导入{Table};
从“./types/input filter/input filter.component”导入{InputFilterComponent};
从“rxjs/Observable”导入{Observable};
@组成部分({
选择器:“[phantom thead filter]”,
模板:“操作”
',
样式URL:['./phantom thead filter.component.css']
})
导出类幻影ADFilterComponent在ViewInit之后实现OnInit{
@输入()cols;
@Input()表:表;
@ViewChildren(InputFilterComponent)过滤器:QueryList;
构造函数(){}
恩戈尼尼特(){
}
ngAfterViewInit(){
const filters=this.filters.map(f=>f.getFilterWatcher());
可观察。组合测试(过滤器)
.map((筛选器:any[])=>{
filters.map((filter)=>{
回流过滤器;
});
});
}
}
从“@angular/core”导入{Component,Input,OnInit};
从“@angular/forms”导入{FormControl}”;
从“../../../models/Table”导入{Table};
@组成部分({
选择器:“输入筛选器”,
模板:“”,
样式URL:['./输入过滤器.component.css']
})
导出类InputFilterComponent在Init上实现{
inputControl=新表单控件();
@输入()列;
@Input()表:表;
更改过滤器:任何;
构造函数(){}
恩戈尼尼特(){
this.changeFilter=this.inputControl.valueChanges
.skip(1)
.distinctUntilChanged()
.debounceTime(400)
.map((值:字符串)=>{
返回值;
}).startWith(空);
}
getFilterWatcher(){
返回此.changeFilter;
}
恩贡德斯特罗(){
this.changeFilter.unsubscribe();
}

}
CombineTest等待所有可观察对象发出其第一个值

如果即使未设置所有筛选器也要进行查询,则可以使用
startWith
提供虚拟的第一个空值

const filters = this.filters.map(f => f.changeFilter.startWith(null));

Observable.combineLatest(...filters)
          .map(( filters : ActiveFilter[] ) => {
                // All filters are defined, but not all have values [null, 'search', null, null]
          });

您可以尝试以下方法:

let listObs = [];
const searchTermA = Observable.fromEvent(this.searchInputARef.nativeElement, 'keyup').debounceTime(300).distinctUntilChanged();
listObs = [...listObs, searchTermA];

const searchTermB = Observable.fromEvent(this.searchInputBRef.nativeElement, 'keyup').debounceTime(300).distinctUntilChanged();
listObs = [...listObs, searchTermB];

Observable.combineLatest(list).subscribe(latestValues => {
    const [searchTermAValue, searchTermBValue] = latestValues;
});

你能提供更多的代码吗?你的可观察的搜索输入和组合测试在你组合它的地方。有没有办法添加动态过滤器。它会更有效吗?还有来自博客的例子对我来说不起作用:我更新了我的代码。你可以有一个可观察的过滤器,它会发出已启用过滤器的列表。然后你可以组合它们并连接它们
changeFilter
到api请求。为什么我当前的代码不起作用?我发现了这篇文章:这是唯一正确的方法吗?或者使用rxjs我们可以获得相同的结果