简体   繁体   English

AngularJs重定向

[英]AngularJs redirect

I've got a table where 1 row has to link to something else. 我有一张表,其中1 row必须链接到其他东西。

Right now I've got it like this: 现在我有这样的事情:

<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>

The last td has to link to something else. 最后的td必须链接到其他东西。 But right now all links to the same how could I manage that? 但是,现在所有链接都相同,我该如何管理呢?

(In the ng-click method I redirect)

您可以使用$last (boolean)并将其作为参数传递给ng-click函数,然后根据是否为最后一个元素来重定向用户。

<td ng-show="homeCtrl.canEdit" ng-click="homeCtrl.redirectToEdit(employee.EmployeeId, $last)"><span class="glyphicon glyphicon-pencil"></span></td>

尝试这个

 <td ng-show="$last  && homeCtrl.canEdit" ng-click="homeCtrl.redirectToEdit(employee.EmployeeId)"><span class="glyphicon glyphicon-pencil"></span></td>

There are two problems in your current implementation. 当前的实现中存在两个问题。

  1. You could use $last to bind click event to fire on last element. 您可以使用$last绑定click事件以在最后一个元素上触发。 I had put $last before homeCtrl.redirectToEdit function call because that will make sure until $last (last element of ng-repeat appears) row gets clicked then only it will call the next function. 我将$last放在homeCtrl.redirectToEdit函数调用之前,因为这将确保直到单击$last (出现ng-repeat最后一个元素)行,然后才调用下一个函数。

     ng-click="$last && homeCtrl.redirectToEdit(employee.EmployeeId);$event.stopPropagation()" 
  2. I think you are facing problem related to event bubbling, where you have click event binded to child and parent element. 我认为您面临与event冒泡相关的问题,在该event ,单击事件已绑定到子元素和父元素。 So when click event happen inside inner element it will propagate that event to its parent. 因此,当内部元素内部发生click事件时,它将将该事件传播到其父对象。 To avoid such unwanted behaviour you should stop inner event bubbling just by calling $event.stopPropagation() on inner td . 为了避免这种不良行为,您应该仅通过在内部td上调用$event.stopPropagation()来停止内部事件冒泡。

You can use $last and a condition: 您可以使用$last和一个条件:

<td ng-show="homeCtrl.canEdit && !$last" ng-click="homeCtrl.redirectToEdit(employee.EmployeeId)"><span class="glyphicon glyphicon-pencil"></span></td>
<td ng-show="homeCtrl.canEdit && $last" ng-click="homeCtrl.somethingDifferent(employee.EmployeeId)"><span class="glyphicon glyphicon-pencil"></span></td>

Ideally, You should set the ng-click to be per td because setting an ng-click to the full row will take precedence over any ng-click s inside it. 理想情况下,应将ng-click设置为td因为将ng-click设置为整行将优先于其中的所有ng-click Try this: 尝试这个:

<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">
    <td ng-click="homeCtrl.redirectToShow(employee.EmployeeId)">{{  employee.Role }}</td>
    <td ng-click="homeCtrl.redirectToShow(employee.EmployeeId)">{{  employee.Company }}</td>
    <td ng-click="homeCtrl.redirectToShow(employee.EmployeeId)">{{  employee.FirstName }}</td>
    <td ng-click="homeCtrl.redirectToShow(employee.EmployeeId)">{{  employee.LastName }}</td>
    <td ng-show="homeCtrl.canEdit" ng-click="homeCtrl.redirectToEdit(employee.EmployeeId)"><span class="glyphicon glyphicon-pencil"></span></td>
</tr>
</tbody>

That said, if you want to keep your markup the way it is, you can look at this StackOverflow answer about stopping bubbling: Howto: div with onclick inside another div with onclick javascript 就是说,如果您希望按原样保留标记,则可以查看有关停止冒泡的StackOverflow答案: Howto:带有onclick的div和带有onclick javascript的另一个div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM