首页 > 解决方案 > FormControl valuechanges 第一个输入发出空字符串

问题描述

private filtering(): void {
  this.filteredOptions$ = this.formC.valueChanges.pipe(
        debounceTime(800),
        startWith(''),
        map(value => this._filter(value))
    );
}

此函数应将表单控件值赋予 this._filter 函数。但是在第一次输入时,它会给出空字符串,因此它不会过滤任何内容。

只是想不通为什么..

标签: angularpipeform-control

解决方案


如果我们想在订阅时获得一个实际值,我们可以自己发出它。

尝试这个:

  this.filteredOptions$ = defer(() =>
      this.formC.valueChanges.pipe(startWith(this.formC.value))
    ).pipe(debounceTime(800),map(value => this._filter(value)))

推荐阅读