指定Observable在angularfire2列表中执行动态查询

时间:2016-11-10 17:33:13

标签: angular firebase firebase-realtime-database rxjs angularfire2

有没有办法将整个查询作为可观察对象传递?此方法最终会使用startAt两次调用查询,然后使用endAt调用一次。我基本上想用通配符进行搜索。

import { Component, OnInit } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
import { Subject } from 'rxjs';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {

  private sources: FirebaseListObservable<any>;
  private uid: string;
  private searchSubject: Subject<SearchSubject>;
  private search: Search;
  constructor(private af: AngularFire) {
    this.search = new Search("");
    this.searchSubject = new Subject<SearchSubject>();
    af.auth.subscribe(d => {
      this.uid = d.uid;
      this.sources = af.database.list('/sources', {
        query: {
          orderByChild: 'name',
          startAt: this.searchSubject.map(d=>{
            return d.startAt;
          }),
          endAt: this.searchSubject.map(d=>{
            return d.endAt;
          })
        }
      });
      this.sources.subscribe(d=>{
        console.log(d);
      });
    });
  }
  ngOnInit() {
  }
  onSearch(form: any): void {
    this.searchSubject.next(new SearchSubject('name',this.search.value,this.search.value+"\uf8ff"));
  }
}

export class Search {
  constructor(
    public value: string
  ) {  }
}
export class SearchSubject {
  constructor(
    public orderByChild:string,
    public startAt: string,
    public endAt: string
  ) {  }
}

0 个答案:

没有答案