简体   繁体   English

使用Angularfire2和Firestore自动完成

[英]Autocomplete with Angularfire2 and Firestore

Does someone know how to make an autocomplete function with Angularfire2 and Firestore please? 有人知道如何使用Angularfire2和Firestore进行自动完成功能吗?

I mean, you have an input, and when you start to write, you make a query and you get desired results in response. 我的意思是,您有一个输入,当您开始写作时,您进行查询并得到期望的结果作为响应。

I have no idea how to do that. 我不知道该怎么做。 I had some lame attempts with query.where but it didn't go well. 我在query.where上做了一些la脚的尝试,但是进展不顺利。

For now I just get all the results on load and filter the data, but I'm really not happy with that solution. 现在,我只是获得了所有加载和过滤数据的结果,但是我对这种解决方案真的不满意。

Any help would be appreciated. 任何帮助,将不胜感激。 Better yet, with some explanation, because I'm lost in the whole angularfire and firestore thing. 更好的是,有了一些解释,因为我迷失在整个angularfire和firestore中。

I think this can be achieved using Behavior subject and switch map operator. 我认为可以使用行为主题和切换图运算符来实现。

Steps: 1. define a behavior subject with null as initial value. 步骤:1.定义一个以null为初始值的行为主体。 2. switchMap this behavioural subject to another observalble that actually does the data fetch from firestore. 2.将此行为主体映射到另一个从火场实际获取数据的观察对象。 3. Define a debounce time is needed to wait for a particular time as the user types into the Input. 3.定义在用户键入输入内容时需要等待特定时间的去抖动时间。

refer the offical github example for angularfire2 as well. 也可以参考Angularfire2的官方github示例。 I hope this will work for you. 希望这对您有用。

 inputFilter$ = new BehaviorSubject(null);
    fireStoreResult$ = inputFiler$.swithMap((inputText) => {
      afs.collection('items', ref => {
        let query : firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
        if (inputText) { query = query.where('someParameter', '==', inputText) };
        return query;
      }).valueChanges()
    })

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM