首页 > 解决方案 > 如何在打字稿中单击表格中的按钮时获取动态表格行数据

问题描述

我正在使用动态表,每行都有一个按钮,单击按钮时,我需要单击相应行按钮的数据。

我已经尝试过(点击)事件绑定,我得到的输出是未定义的。

<table>
<thead> <tr> <th> Name </th> <th> Company </th> </tr> </thead>
<tr *ngFor = "let employee of employees" (click) = "removeEmployee(row)">
            <td> <input type="text" [(ngModel)]= "employee.name"></td>
            <td> <input type="text" [(ngModel)] = "employee.companyName"> </td>
</tr>
</table>

.ts 文件

removeEmployee(tr) {
      console.log(tr);
    }

预期:单击按钮时,应输出表格行数据。

实际:显示未定义。

标签: angulartypescripthtml-table

解决方案


更改员工行:

<tr *ngFor = "let employee of employees" (click) = "removeEmployee(employee)">

推荐阅读