【发布时间】:2020-09-11 18:57:48
【问题描述】:
我已将open() 方法附加到我的ng-select 元素,以便使用来自外部API 的数据填充它。
我遇到的问题:如果我打开下拉列表 5 次然后我输入一个字母,它将向服务器发出 5 个 http 请求以填充数据。当我使用虚拟滚动功能时,同样的问题。
component.html
<ng-select [items]="filterValuesBuffer"
[typeahead]="filterValuesInput$[filter.name]"
[virtualScroll]="true"
[multiple]="true"
[closeOnSelect]="false"
[loading]="filterValuesLoading[filter.name]"
[(ngModel)]="filter.filter_values"
(scrollToEnd)="onScrollToEnd(filter.name)"
(open)="onFilterOpen(filter.name)"
typeToSearchText="No values found"
bindLabel="name">
</ng-select>
component.ts
filterValuesInput$: Map<string, Subject<string>[]> = new Map();
onFilterOpen(filterName) {
this.filterValuesLoading[filterName] = true;
this.getFilterValues(filterName, '')
.subscribe(res => {
this.afterKey = res.after_key;
this.filterValuesLoading[filterName] = false;
this.filterValuesBuffer = res.filter_values;
});
}
getFilterValues(filterName, afterKey) {
return this.filterValuesInput$[filterName].pipe(
tap(() => this.filterValuesLoading[filterName] = true),
startWith(''),
distinctUntilChanged(),
switchMap(term => this.search.getFilterValues(filterName, '' + term, '' + afterKey)),
)
}
如何防止这种行为?
编辑虚拟卷轴:
(scrollToEnd)="fetchMore(filter.name)"
fetchMore(filterName) {
this.filterValuesLoading[filterName] = true;
this.getFilterValues(filterName, this.afterKey)
.subscribe(res => {
this.afterKey = res.after_key;
this.filterValuesLoading[filterName] = false;
this.filterValuesBuffer = this.filterValuesBuffer.concat(res.filter_values);
})
}
【问题讨论】:
-
你用过
share()方法吗? -
我没有注意到使用
share()有任何区别 -
试过我的解决方案了吗?
标签: angular rxjs behaviorsubject rxjs-observables