javascript - Angular 9按钮单击多次触发
问题描述
html
<button (click)="myClickFunction($event)">
Click Me
</button>
app.component.ts
myClickFunction(event) {
//just added console.log which will display the event details
console.log('inside myClickFunction');
}
当我多次单击按钮(名为 click me)时,console.log 会打印多次。无论点击次数如何,如何使其仅在第一次调用?
我尝试了 event.preventdefault(),但出现以下错误:
Cannot read property 'preventDefault' of undefined
任何建议将不胜感激。
解决方案
您可以创建一个计数器来跟踪点击次数并保护点击方法。
private clickCounter = 0;
myClickFunction(event) {
if(this.clickCounter > 0){
return;
}
this.clickCounter++;
//just added console.log which will display the event details
console.log('inside myClickFunction');
}
推荐阅读
- flutter - 关闭和打开应用程序后记住屏幕状态
- node.js - 如何使用`childProcess.spawn`避免异步/承诺反模式?
- keras - 神经网络的输入(反向传播)是否正确实施?
- java - 预期为 BEGIN_OBJECT,但在第 1 行第 14 列路径 $ 与 Object 和 SQLite 为 NUMBER
- c# - 是否可以传递 IIncludableQueryable 对象并将其应用于 DbSet
- javascript - Express.js 在动态路由上提供静态文件
- scala - scala列表过滤中的复杂问题
- html - 进度条未填充进度条脚本标记
- r - 根据来自另一个数据集的时间间隔汇总一个数据集中的数据
- r - Fama MacBeth 与池化 OLS