简体   繁体   English

在 observable 中添加延迟会在 Angular rxjs 中返回部分数据

[英]Adding delay in observable returns partial data in Angular rxjs

In my code I need to add delay by using timer(500) .在我的代码中,我需要使用timer(500)添加延迟。 But the issue is that it returns partial data.但问题是它返回部分数据。 It is returning 2 fields while actual data has 17 fields.它返回 2 个字段,而实际数据有 17 个字段。 I have attached my code.我附上了我的代码。 please see it.请看。 Thank you谢谢

Returned value:返回值:

 ['booking_display_id', 'edit']

Expected value:期望值:

 ['booking_display_id', 'bookingstatus', 'b_contactname', 'member', 'b_emailaddress', 'b_mobilenumber', 'startdate', 'enddate', 'duration', 'bookingguest', 'guestnotes', 'vouchers', 'paypalpaymentpdt', 'totalCost', 'canPay', 'canCancel', 'edit']

 this.displayedColumns = combineLatest(this.table.columns.reduce((observables: Observable<boolean>[], col) => { // handle showIf property of column const show = col.showIf(this.injector, this.route.queryParamMap); observables.push(show instanceof Observable ? show : of(show)); return observables; }, []), timer(500)).pipe( map(showCols => { const cols = this.table.columns.filter((c, i) => showCols[i]) .map(c => c.id); this.editEnabled && cols.push('edit'); this.deleteEnabled && cols.push('delete'); console.log('cols', cols) return cols; }) );

You need to merge the delay event and your observable event.您需要合并延迟事件和可观察事件。

let c = [
  'booking_display_id',
  'bookingstatus',
  'b_contactname',
  'member',
  'b_emailaddress',
  'b_mobilenumber',
  'startdate',
  'enddate',
  'duration',
  'bookingguest',
  'guestnotes',
  'vouchers',
  'paypalpaymentpdt',
  'totalCost',
  'canPay',
  'canCancel',
  'edit',
];

let bsObs: BehaviorSubject<String> = new BehaviorSubject<String>('');
let obs: Observable<String> = bsObs.asObservable();

obs
  .pipe(
    concatMap((x) => of(x).pipe(delay(500)))
  )
  .subscribe((s) => console.log(s));

c.forEach((c) => bsObs.next(c));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM