shA*_*kur 5 select autocomplete rxjs dropdown angular
我正在使用ng-select自定义服务器端搜索来根据用户键入的内容加载数据。目前,它仅在实际按下关键字时才起作用。
<ng-select [items]="filterValues$ | async"
[typeahead]="filterValuesInput$"
[multiple]="true"
(open)="getFilterValues(pref.id)"
[loading]="filterValuesLoading"
bindLabel="name"
[(ngModel)]="filter_values">
</ng-select>
Run Code Online (Sandbox Code Playgroud)
我想在选择下拉列表打开时触发 API 请求,即使没有提供搜索词。
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)
))
)
);
}
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
小智 4
发生这种情况是因为 concat 没有发出第二个可观察值的任何值。您可以尝试使用 startWith 运算符进行初始加载
getFilterValues(filterId) {
this.filterValues$ = concat(
of([]), // default items
this.filterValuesInput$.pipe(
startWith(''),
debounce(300),
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)
))
)
);
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助