如何在打字稿中单击表内的按钮以获取动态表行数据

时间:2019-10-24 11:16:39

标签: angular typescript html-table

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

我尝试了(单击)事件绑定,但我得到的输出未定义。

<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);
    }

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

实际:显示未定义。

2 个答案:

答案 0 :(得分:2)

更改员工行:

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

答案 1 :(得分:0)

这是您的解决方案

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

.ts文件

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