angular - Angular如何在QueryList中找到哪个元素被点击
问题描述
我有一组使用 ViewChildren 的表格单元格(动态创建的 td html 元素)的 ElmentRef(QueryList)。我调试并有可用的元素集。
当我单击特定的 td html 元素时,我会调用一个函数,在该函数中我需要找到 ElementRef(QueryList) 中的哪个元素被单击。我怎样才能做到这一点?
组件.html
<table>
<tr *ngFor="let i of Arr">
<ng-container *ngFor="let j of Arr">
<td #tdID (click)="cellClicked(tdID)">
</td>
</ng-container>
</tr>
</table>
组件.ts
Arr =[1,2,3];
@ViewChildren('tdID') divs:QueryList<ElementRef>;
cellClicked(cell) {
console.log("Cell clicked"+cell);
//Help find here which element in the divs QueryList matches "cell"
}
解决方案
<table>
<tr *ngFor="let i of Arr">
<ng-container *ngFor="let j of Arr">
<td #tdID (click)="cellClicked(tdID, i, j)">
</td>
</ng-container>
</tr>
</table>
In you .ts file
cellClicked(tdID, i, j) {
console.log(tdID, i, j)
}
推荐阅读
- typescript - 基于参数以不同的返回值模拟 Jest
- wordpress - 帖子编辑器中 URL 弹出框的特定过滤器
- intellij-idea - 删除以前版本的 IntelliJ 是否安全?
- java - 排除@SpringBootApplication
- methods - 你能结合方法并捕获输出吗?
- c# - 在 NetCore 上使用 Contains 构建 Lambda 表达式
- d3.js - D3 v5:多线图,无法绘制多条线
- python - 熊猫 - .loc / lambda / 时间序列
- python - 如何使用密码将 Superset 连接到 Redis?
- github - 在 Bitbucket 上创建存储库之前