I want to send a request to my server to filter a list based on a date selected by the user, but I want to use the debounce for this in order not to spam the backend.
My TS Component
@ViewChild('searchInputDateStart', {static: false}) searchInputDateStart: ElementRef;
ngAfterViewInit() {
fromEvent(this.searchInputDateStart.nativeElement, 'dateInput').pipe(
map((event: any) => {
return event.target.value;
}),
debounceTime(500)
).subscribe((filterValue: string) => {
this.startDateToFilterFor = filterValue;
this.sendRequest();
});
}
My Template
<mat-form-field appearance="fill"
fxFlex.lt-md="20"
fxFlex.gt-sm="40"
color="accent">
<input id="inputDateStart"
matInput
#searchInputDateStart
[matDatepicker]="pickerStart">
<mat-datepicker-toggle matSuffix
[for]="pickerStart"></mat-datepicker-toggle>
<mat-datepicker #pickerStart></mat-datepicker>
</mat-form-field>
The same approach worked for the 'keyup' event on a normal text field, exactly like this answer but not for a datepicker