首页 > 解决方案 > 如何在鼠标事件绑定中传递参数

问题描述

我使用基于行和列索引的参数为每个单元格渲染表格并添加(mouseenter)事件。

<table>
 <tr *ngFor="let row of rows; let rowIndex = index;">
  <td *ngFor="let column of columns; let columnIndex = index;"
   (mouseenter)="mouseEnterHandler(rowIndex, columnIndex)">
  </td>
 </tr>
</table>

mouseEnterHandler(rowIndex, columnIndex) {}

但是,这会导致性能问题,这就是为什么我想使用 ngZone 在 angular 之外运行 mouseenter。

this.zone.runOutsizeAngular(() => {
 this.element.nativeElement.addEventListener('mouseenter', this.mouseEnterHandler.bind(this));
});

但是现在我将无法访问当前的 mouseenter 行和列索引。是否可以在鼠标输入事件期间获取这些参数?

标签: angular

解决方案


在你的功能中,你可以做这样的事情

mouseEnterHandler(rowIndex, columnIndex){
  console.log(rowIndex);
  console.log(columnIndex);
}


推荐阅读