I'm using ng-select custom server-side search to load data based on what the user types. Currently it only works if a keyword is actually pressed. I'd like to trigger the http request each time the dropdown is opened, even if the search term is empty
component.html
<ng-select [items]="filterValues$ | async"
[typeahead]="filterValuesInput$"
[multiple]="true"
(open)="getFilterValues(pref.id)"
[loading]="filterValuesLoading"
bindLabel="name"
[(ngModel)]="filter_values">
</ng-select>
component.ts
getFilterValues(filterId) {
this.filterValues$ = concat(
of([]), // default items
this.filterValuesInput$.pipe(
distinctUntilChanged(),
tap(() => this.filterValuesLoading = true),
switchMap(term => this.service.getFilterValues(filterName, '' + term).pipe(
map(res => res.filter_values),
catchError(() => of([])), // empty list on error
tap(() => this.filterValuesLoading = false)
))
)
);
}
I'm trying to understand how switchMap() triggers the request only after an input value is typed. How do I make it trigger the service's method each time the getFilterValues method is called?