如何在使用observable发出新请求之前保留先前http请求的数据?

时间:2017-03-21 13:46:00

标签: angular rxjs observable

父组件

this.statisticsService.getData().subscribe(data =>  
   this.processData(data));

processData(data: DataWrapper) {
  if (!isNullOrUndefined(data) && data.table.rows.length > 1) {
    this.noDataAvailableFlag = false;
    this._statisticsData.next(data);
  } else { // If no data available from backend
    console.log('no data available');
    this.handleNoDataAvailable();
  }
}

模板

<div *ngIf="isDataAvailable" class="table-box clearfix default-style [ngClass]="backgroundColour">
  <div [hidden] ="!noDataAvailableFlag">
    <span class="no-data-message">{{'DETAILS.No_Data_Available' | translate}}</span>
  </div>
  <div [hidden] ="noDataAvailableFlag" detail-table [_statisticsData]="statisticsData" [maximiser]="maximiser" [queryParams]="filterService.getFullQueryParams()"
     (onVariationClicked$)="onVariationClicked($event)"></div>
  <div [hidden] ="noDataAvailableFlag" class="graph-holder clearfix" [ngClass]="{'opened': maximiser.isMaximised}" detail-graph
     [_statisticsData]="statisticsData"
     [tableVariation]="selectedVariationTitle"></div>

子组件ngOnit方法。

ngOnInit() {
  this.windowWidth = window.innerWidth;
  this._statisticsData.subscribe(data => {
  if (!isNullOrUndefined(data)) {
    this.processData(data);
  }
 });
}

如果没有可用的数据,我不想清除该表。我希望表格中显示前一个请求的数据。请建议我如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

过滤器的用途:

this._statisticsData
  .filter(data => !isNullOrUndefined(data))
  .subscribe(data => {
    this.processData(data);
  });