我在表格行上有一个点击事件,在这一行中还有一个带有点击事件的删除按钮。当我单击删除按钮时,该行上的单击事件也会被触发。
这是我的代码。
<tbody>
<tr ng-repeat="user in users" class="repeat-animation" ng-click="showUser(user, $index)">
<td>{{user.firstname}}</td>
<td>{{user.lastname}}</td>
<td>{{user.email}}</td>
<td><button class="btn red btn-sm" ng-click="deleteUser(user.id, $index)">Delete</button></td>
</tr>
</tbody>
如何防止在单击表格单元格中的删除按钮时触发 showUser
事件?
最佳答案
ngClick 指令(以及所有其他事件指令)创建在相同范围内可用的 $event
变量。此变量是对 JS event
对象的引用,可用于调用 stopPropagation()
:
<table>
<tr ng-repeat="user in users" ng-click="showUser(user)">
<td>{{user.firstname}}</td>
<td>{{user.lastname}}</td>
<td>
<button class="btn" ng-click="deleteUser(user.id, $index); $event.stopPropagation();">
Delete
</button>
</td>
</tr>
</table>
关于javascript - AngularJS ng-click stopPropagation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57305445/