角度材料表文本过滤 - rxjs - 合并http获取结果与过滤器主题

角度材料表文本过滤 - rxjs - 合并http获取结果与过滤器主题

问题描述:

我想应用Angular Material Text Filtering的示例使用来自http get调用的数据。角度材料表文本过滤 - rxjs - 合并http获取结果与过滤器主题

export class MyDtoDataSource extends DataSource<IMyDto> { 
     private _filterChange = new BehaviorSubject(''); 
     public get filter(): string { return this._filterChange.value; } 
     public set filter(filter: string) { this._filterChange.next(filter); } 

     constructor(private _apiService: ApiService) { 
      super() 
     } 

     connect(): Observable<IMyDto[]> { 

      const displayDataChanges = [ 
       this._apiService.getMyDtos(), 
       this._filterChange, 
      ]; 

      return Observable 
      .merge(...displayDataChanges) 
      .map((dtos: IMyDto[]) => dtos.filter(dto => dto.categories.map(i => i.name).includes(this.filter))); 
     } 

    disconnect() { 
    } 
} 

不过,我想有我地图的问题,因为我得到的运行时错误dtos.filter is not a function

+0

'_filterChange'会发出'string',而不是一个'IMyDto []'所以包括它在'merge'会看到'string'值合并到可观察到的数据流,影响您所看到的错误。您很可能想使用['withLatestFrom'](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-withLatestFrom)而不是'merge'。 – cartant

韩国社交协会以@cartant为点我到正确的方向。

connect(): Observable<IMyDto[]> { 
    return Observable 
     .combineLatest(this._apiService.getMyDtos(), this._filterChange) 
     .map(([dtos, filter]) => dtos.filter(dto => dto.categories.map(i => i.name).includes(filter))); 
}