【发布时间】:2020-02-12 16:08:17
【问题描述】:
我需要在搜索数据时显示加载,并在没有找到记录时显示文本。
我有以下 html 摘录:
<input
matInput
[matAutocomplete]="auto"
[formControl]="formControl"
[formlyAttributes]="field"
[placeholder]="to.placeholder"
[errorStateMatcher]="errorStateMatcher"
/>
<mat-icon class="arrow-autocomplete">arrow_drop_down</mat-icon>
<mat-autocomplete
#auto="matAutocomplete"
[displayWith]="displayFn.bind(this)">
<mat-option *ngIf="isLoading" class="is-loading">
<mat-spinner diameter="25"></mat-spinner>
</mat-option>
<ng-container *ngIf="!isLoading">
<mat-option *ngFor="let value of result$ | async" [value]="value">
<ng-container *ngFor="let ky of to.filterKeys; let index = index">
{{
index !== to.filterKeys.length - 1
? value[ky] + ' - '
: value[ky]
}}
</ng-container>
<ng-container *ngIf="!to.filterKeys">
{{ value }}
</ng-container>
</mat-option>
</ng-container>
</mat-autocomplete>
Component.ts 代码:
ngOnInit(): void {
super.ngOnInit();
this._unsubscribeAll = new Subject();
this.isLoading = false;
this.result$ = this.formControl.valueChanges.pipe(
takeUntil(this._unsubscribeAll),
debounceTime(300),
startWith(''),
tap(() => {
this.isLoading = true
}),
switchMap(term => this.to.filter(term))
);
}
对于加载,我会用tap()的方法来控制它,但是我的问题是如何知道switchMap(term => this.to.filter(term))里面的请求已经完成,所以我将加载变量设置为false?
我的第二个问题是如何在服务器返回一个空数组时显示未找到的注册消息,因为我正在使用异步。
【问题讨论】:
标签: angular rxjs angular-material