angular - 如何在角度 5 的去抖时间上达到组件的功能?
问题描述
我如何以角度设置组件功能的去抖动时间。实际上我从 selectTableRow() 方法中找到了一个 api。当我选择 tr 然后点击 api 但是当我选择多个 tr 然后多个请求将发送到服务器。
我希望当我快速选择多个表行时,只发送一个请求,例如(自动完成搜索)。
HTML
<tr *ngFor="let data of tableData"
(click)="selectTableRows($event, data)"
[ngClass]="{'row-highlight': isRowSelected(data.id)}">
<td>
<span>{{data.name}}</span>
</td>
</tr>
方法
selectTableRows(event, rowData) {
//Hit api from here
}
解决方案
要解决您的问题,请使用lodash库的 debounce 方法。
npm i --save lodash
在 .ts 文件顶部导入debounce
import {debounce} from 'lodash';
像这样更新您的功能:
private debouncedFunction = null;
selectTableRows(event, rowData) {
if (this.debouncedFunction) {
this.debouncedFunction.cancel();
}
this.debouncedFunction = debounce(() => {
console.log('selectTableRows', rowData);
// make your API call here.
}, 2000);
this.debouncedFunction();
}
}
推荐阅读
- node.js - 如何将 IBM Cloud Functions 上的操作与数据库连接起来?
- xaml - 如何在 Xbox 中实现评级控制?
- php - Notepad++语法高亮——不同情况下的php关键字
- ios - xcode 10 迁移错误获取段错误 11
- webforms - 如何将 Microsoft.AspNetCore.Identity 与旧的 Web 表单项目一起使用?
- regex - 使用包含 / 的字符串进行 Graylog 搜索
- maven - 了解 Maven 标签
- html - 在内联元素的下一行显示文本
- variables - 反应原生一个变量更新是另一个变量值
- excel - 让我的代码等到光标加载 MS Excel 2016