我正在使用ng-select自定义服务器端搜索来加载数据,无论用户是否提供了搜索词。
组件.html
<ng-select [items]="filterValues$ | async"
[typeahead]="filterValuesInput$"
[multiple]="true"
(open)="getFilterValues(pref.id)"
[loading]="filterValuesLoading"
bindLabel="name"
[(ngModel)]="filter_values">
</ng-select>
组件.ts
getFilterValues(filterName) {
this.filterValues$ = concat(
of([]), // default items
this.filterValuesInput$.pipe(
startWith([]),
distinctUntilChanged(),
tap(() => this.filterValuesLoading = true),
switchMap(term => this.preferencesService.getFilterValues(filterName, '' + term).pipe(
map(res => res.filter_values),
catchError(() => of([])), // empty list on error
tap(() => this.filterValuesLoading = false)
))
)
);
}
我注意到的问题是,每当我打开选择下拉菜单时,它都会触发控制台错误:
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'loading: false'. Current value: 'loading: true'.
之后,如果提供了搜索词,则由 this.filterValuesLoading 触发的加载微调器可以正常工作。这里有什么问题?谢谢!