RXJS日期范围选择器始终等待启动和结束可观察到
我有一个角度材料日期选择器,并且我正在尝试在提交查询
component.html
<mat-form-field>
<mat-label>Date Range</mat-label>
<button mat-button matSuffix mat-icon-button aria-label="Clear" type="button" (click)="clearDate()">
<mat-icon>close</mat-icon>
</button>
<mat-date-range-input [formGroup]="dateRangeGroup" [rangePicker]="picker">
<input matEndDate formControlName="end" placeholder="End date">
<input matStartDate formControlName="start" placeholder="Start date">
</mat-date-range-input>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-date-range-picker #picker></mat-date-range-picker>
</mat-form-field>
component.ts
constructor(
public svcA: ServiceA,
public svcB: ServiceB,
) {
// ignore this, just for debugging
this.dateRangeGroup.controls['start'].valueChanges.pipe(filter(res => res !== null)).subscribe(val => console.log('start date', val));
this.dateRangeGroup.controls['end'].valueChanges.pipe(filter(res => res !== null)).subscribe(val => console.log('end date', val));
combineLatest([
this.dateRangeGroup.controls['start'].valueChanges.pipe(filter(res => res !== null)),
this.dateRangeGroup.controls['end'].valueChanges.pipe(filter(res => res !== null))
])
.subscribe(([start, end]) => {
console.log(`combineLatest ${start} - ${end}`);
this.svcA.sendQuery(start, end);
},
err => {
console.log('error', err)
},
() => {
console.log('complete');
});
// set default date range search to 30 days
this.clearDate();
}
combineLatest
之前,请获取开始日期和结束日期可观察到的可观察日期。它等待两者都首先发射事件,只是它在触发第一个事件后排放了每个单独的日期选择(选择“开始&amp;结束日期”)。当查询通过用户选择日期提交中的一半时,这会引起不愉快的UI
体验?
I have an angular material date picker and i'm trying get the start date and end date observables before I submit my query
component.html
<mat-form-field>
<mat-label>Date Range</mat-label>
<button mat-button matSuffix mat-icon-button aria-label="Clear" type="button" (click)="clearDate()">
<mat-icon>close</mat-icon>
</button>
<mat-date-range-input [formGroup]="dateRangeGroup" [rangePicker]="picker">
<input matEndDate formControlName="end" placeholder="End date">
<input matStartDate formControlName="start" placeholder="Start date">
</mat-date-range-input>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-date-range-picker #picker></mat-date-range-picker>
</mat-form-field>
component.ts
constructor(
public svcA: ServiceA,
public svcB: ServiceB,
) {
// ignore this, just for debugging
this.dateRangeGroup.controls['start'].valueChanges.pipe(filter(res => res !== null)).subscribe(val => console.log('start date', val));
this.dateRangeGroup.controls['end'].valueChanges.pipe(filter(res => res !== null)).subscribe(val => console.log('end date', val));
combineLatest([
this.dateRangeGroup.controls['start'].valueChanges.pipe(filter(res => res !== null)),
this.dateRangeGroup.controls['end'].valueChanges.pipe(filter(res => res !== null))
])
.subscribe(([start, end]) => {
console.log(`combineLatest ${start} - ${end}`);
this.svcA.sendQuery(start, end);
},
err => {
console.log('error', err)
},
() => {
console.log('complete');
});
// set default date range search to 30 days
this.clearDate();
}
combineLatest
works perfectly for what I want in the beginning as it waits for both to fire an event first, except it then emits each individual date selection after the first event has been fired (selecting start & end date). This causes an unpleasant UI experience when the query is submitted half way through a user selecting dates
How can I wait for both long lived observables so that I can ensure a start and end date has been selected each time a user decides to change the date ranges?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么要使用FormControl的
Valuechanges
可观察到的内容来处理提交,而不是使用提交按钮。无论如何,如果它是唯一可用的选项,则可以使用rxjs zip 操作员而不是操作员 combineLatestest 一个。
zip
操作员每次等待,直到两个流散发值,然后将它们组合在数组中。I'm not sure why you're handling the submit using the FormControl's
valueChanges
Observables, instead of using a submit button.Anyway, if it's the only available option, you can use the RxJS zip operator instead of the combineLatest one.
The
zip
operator waits each time until both of the streams emit a value, then combine them in an array.