我有一个输入,当用户键入时,它会执行实时搜索。例如,假设他搜索了以下内容:
车
结果将是:
[
{
id: "1"
name: "Ferrari"
},
{
id: "2"
name: "Porsche"
}
]
我能够成功地做到这一点,这是如何做到的:
class WordComponent {
word: Subject<string> = new Subject<string>();
result: any[] = [];
constructor(private http: Http) {
this.subscribe();
}
subscribe(): void {
this.word.debounceTime(400)
.distinctUntilChanged()
.switchMap((word: string): Observable<any[]> => this.http.get(word))
.subscribe((result: any[]): any[] => this.result = result);
}
search(event: any): void {
this.word.next(event.target.value);
}
}
和观点:
<input type="text" placeholder="Word" (keyup)="search($event)">
我希望用户能够同时输入多个单词并分别对每个单词进行实时搜索。例如,假设他搜索了以下内容:
汽车食品太阳
car的结果将是:
[
{
id: "1"
name: "Ferrari"
},
{
id: "2"
name: "Porsche"
}
]
食物的结果将是:
[
{
id: "3"
name: "egg"
},
{
id: "4"
name: "cheese"
}
]
sun的结果是:
[
{
id: "5"
name: "star"
},
{
id: "6"
name: "sky"
}
]
并合并每个单词的结果,在这种情况下,它看起来像这样:
[
[{
id: "1"
name: "Ferrari"
},
{
id: "2"
name: "Porsche"
}
],
[{
id: "3"
name: "egg"
},
{
id: "4"
name: "cheese"
}
],
[{
id: "5"
name: "star"
},
{
id: "6"
name: "sky"
}
]
]
但是,假设用户在键入所有单词并执行搜索后,希望更改其中之一。只需要重新搜索改变的词,最终结果的合并也必须重新进行。
我仍然不知道 rxjs 的所有功能,我不知道实现这一目标的理想方法是什么。如果您需要参考,显示目的站点有一个非常相似的搜索引擎。