用默认值初始化CombineLatest

时间:2019-10-24 11:36:18

标签: angular rxjs

根据combinateLatest上的文档,我无法在启动时将其初始化为包含值。这导致我的组件为空。有什么办法可以解决,以便默认显示this.projects?

  ngOnInit() {
    this.filtered =
      combineLatest(of(this.projects), this.searchService.observable)
        .pipe(
          map(([projects, searchString]: [{ name: string, preview: string, text: string }[], string]) =>
            projects.filter(x => x.name.toLowerCase().includes(searchString.toLowerCase()))
          ));

    this.filtered.subscribe();
  }

1 个答案:

答案 0 :(得分:2)

是的,请使用startWith运算符。

  ngOnInit() {
    this.filtered =
      combineLatest(of(this.projects), this.searchService.observable.pipe(startWith('')))
        .pipe(
          map(([projects, searchString]: [{ name: string, preview: string, text: string }[], string]) =>
            projects.filter(x => x.name.toLowerCase().includes(searchString.toLowerCase()))
          ));

    this.filtered.subscribe();
  }