【问题标题】:RXJS - Filter appears to filter, but results still show unfilteredRXJS - 过滤器似乎已过滤,但结果仍显示未过滤
【发布时间】:2018-12-13 15:22:47
【问题描述】:

我正在使用 RXJS 6.3.3。

我有以下代码应该过滤掉已经选择的小玩意。

如果我有:

Gizmos = "Red", "Blue", "Green"

用户选择“蓝色”。

我应该只看到“红色”和“绿色”作为我的可用选项。

但是我仍然看到“红色”、“蓝色”、“绿色”。

在调试中,过滤器似乎正在运行它将打印的示例:

Red:true Blue:false Green:true

我不确定我缺少什么,因为 UI 仍然显示所有 3 个值。

      <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
        <mat-option *ngFor="let gizmp of filterGizmos | async" [value]="gizmo.value" (onSelectionChange)="gizmoSelectionChange(gizmo)">
          {{gizmo.value}}
        </mat-option>
      </mat-autocomplete>



   filterGizmos: Observable<Gizmo[]>;
   this.filterGizmos = this.filterGizmoData(this.searchTerm.value); 

   filterGizmoData(searchString?: string): Observable<Gizmo[]> {
    let tfp = new GizmoFindParameters();

    if (searchString == undefined) {
      tfp.value = "";
    } else {
      tfp.value = searchString.trim();
    }

    return this.gizmoService.find<Gizmo[]>(tfp)
      .pipe(tap(x =>
        x.filter((y) => {
          console.log(y.value);
          console.log(!this.selectedGizmos.includes(y.value)); 
          return !this.selectedGizmos.includes(y.value);
        })
      )
      );
  };

【问题讨论】:

    标签: angular rxjs angular-material


    【解决方案1】:

    tap 不修改流。它通常用于调试和副作用(不影响流的操作)。

    请改用map

    这是来自tap 的文档:

    对源 Observable 上的每个发射执行副作用,但是 返回一个与源相同的 Observable。

    【讨论】:

    • 谢谢。我没听懂
    猜你喜欢
    • 1970-01-01
    • 2018-07-29
    • 2014-08-11
    • 1970-01-01
    • 2015-10-14
    • 2014-10-23
    • 2021-09-09
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多