AngularJs 重定向

标签 angularjs events angularjs-ng-repeat angularjs-ng-click

我有一个表,其中 1 必须链接到其他内容。

现在我得到的是这样的:

<table class="table table-hover">
    <thead>
    <tr>
        <th>Functie</th>
        <th>Bedrijf</th>
        <th>Naam</th>
        <th>Achternaam</th>
    </tr>
    </thead>
    <tbody ng-show="!homeCtrl.loading">
    <tr ng-repeat="employee in homeCtrl.employees" ng-click="homeCtrl.redirectToShow(employee.EmployeeId)">
        <td>{{  employee.Role }}</td>
        <td>{{  employee.Company }}</td>
        <td>{{  employee.FirstName }}</td>
        <td>{{  employee.LastName }}</td>
        <td ng-show="homeCtrl.canEdit" ng-click="homeCtrl.redirectToEdit(employee.EmployeeId)"><span class="glyphicon glyphicon-pencil"></span></td>
    </tr>
    </tbody>
</table>

最后一个td必须链接到其他内容。但现在所有链接都指向相同的内容,我该如何管理呢?

(In the ng-click method I redirect)

最佳答案

您当前的实现存在两个问题。

  1. 您可以使用 $last 绑定(bind)点击事件以在最后一个元素上触发。我已将 $last 放在 homeCtrl.redirectToEdit 函数调用之前,因为这将确保直到 $last(ng-repeat 的最后一个元素 出现)行被单击,然后只有它会调用下一个函数。

    ng-click="$last && homeCtrl.redirectToEdit(employee.EmployeeId);$event.stopPropagation()" 
    
  2. 我认为您面临与事件冒泡相关的问题,其中您将点击事件绑定(bind)到子元素和父元素。因此,当内部元素内部发生单击事件时,它将将该事件传播到其父元素。为了避免这种不需​​要的行为,您应该通过在内部 td 上调用 $event.stopPropagation() 来停止内部事件冒泡。

关于AngularJs 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36289330/

相关文章:

javascript - AngularJs:$http.post总是返回404错误

c++ - 在没有类型删除的情况下用 C++ 构建事件驱动系统的设计模式?

c++ - 滚动时的滚动条事件?

使用 ng-repeat 数据的 AngularJS UI Bootstrap 弹出窗口

javascript - AngularJS - 根据另一个 Controller 的 ng-repeat 将信息动态插入到 Controller 中

javascript - 返回 promise 后访问工厂服务

javascript - 使用express angularJs和mongoDB,但 Angular 中的ngResource未加载

javascript - 在 html 表中动态渲染非结构化 JSON 数据

javascript - scrollIntoView 影响我的监听器

javascript - 在 AngularJS 的 ng-repeat 中添加等待