【发布时间】:2018-06-07 03:14:04
【问题描述】:
我正在使用 Rxjs 6,它根据表单字段中的 (keyup) 事件过滤 Firebase 返回的 observable。
当用户在表单字段中没有值的情况下一直按退格键时,我遇到了一个问题,然后看起来 observable 似乎在不断刷新。
使用 DistinctUntilChanged() 添加管道似乎没有效果:
Typescript 过滤功能:
updateFilter2() {
const val = this.filteredValue.toLowerCase().toString().trim();
if (this.filteredValue) {
this.loadingIndicator = true;
this.rows = this.svc.getData()
.pipe(
distinctUntilChanged(),
debounceTime(300),
map(_co => _co.filter(_company =>
_company['company'].toLowerCase().trim().indexOf(val) !== -1 || !val
||
_company['name'].toLowerCase().trim().indexOf(val) !== -1 || !val
||
_company['gender'].toLowerCase().trim().indexOf(val) !== -1 || !val
)),
tap(res => {
this.loadingIndicator = false;
})
);
}
else {
this.rows = this.svc.getData()
.pipe(distinctUntilChanged())
this.loadingIndicator = false;
}
this.table.offset = 0;
}
HTML 模板:
<mat-form-field style="padding:8px;">
<input
type='text'
matInput
[(ngModel)] = "filteredValue"
style='padding:8px;margin:15px auto;width:30%;'
placeholder='Type to filter the name column...'
(input)='updateFilter2()'
/>
</mat-form-field>
我有一个 Stackblitz 重现该行为:https://stackblitz.com/edit/angular-nyqcuk
还有其他方法可以解决吗?
谢谢
【问题讨论】:
-
听(输入)事件而不是(键)似乎可以解决问题
-
请不要链接到外部网站以提供您的代码除非您已经直接在您的答案中包含了代码的副本。如果其他网站出现故障、消失或更改其链接结构,则该问题无效。