【发布时间】:2017-03-13 07:16:36
【问题描述】:
我编写了一个管道,它根据给定的查询过滤出一组对象。它工作得很好,但我想做的是直接向这个管道添加一个去抖动函数,而不是如果可能的话将它添加到输入的 keyup 事件中。
我一直在寻找解决方案,但似乎找不到任何特定于我正在寻找的东西。
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'filterBy'
})
export class FilterByPipe implements PipeTransform {
transform(value: any, args: string[]): any[] {
if (!args[0]) {
return value;
}
else if (value) {
return value.filter(item => {
// TODO: Allow args[1] to be null, therefore searching in all object properties
if ((typeof item[args[1]] === 'string' || item[args[1]] instanceof String) && (item[args[1]].toLowerCase().indexOf(args[0].toLowerCase()) !== -1)) {
return true;
}
});
}
}
}
关于如何在这个管道中实现它有什么想法吗?
【问题讨论】:
-
你想在哪里应用去抖动
-
@PatrickJane 不确定它需要去哪里。
-
为什么需要去抖?
-
@PatrickJane 所以它不会在每次击键时过滤可能包含数百个项目的列表..
标签: angular typescript debouncing angular2-pipe