在<tr>元素中设置ng-href

cap*_*rad 19 angularjs

以下代码使client.name成为客户端中每个客户端上的锚点.我感兴趣的是将整个<tr>元素作为该链接.ng-href不能在<tr>元素上工作..我能做什么才能使整个行实例化的单个链接ng-href

<tr ng-repeat="client in clients">
    <td><a ng-href="#/user/{{client.tagid}}">{{client.firstname}}</a></td>
    <td>{{client.lastname}}</td>
    <td>{{client.inumber}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

我想要做的就是这样......当然这不起作用..

<a ng-href="#/user/{{client.tagid}}">
    <tr ng-repeat="client in clients">
        <td>{{client.firstname}}</td>
        <td>{{client.lastname}}</td>
        <td>{{client.inumber}}</td>
    </tr>
</a>
Run Code Online (Sandbox Code Playgroud)

要么

<tr ng-repeat="client in clients" ng-href="#/user/{{client.tagid}}">
    <td>{{client.firstname}}</td>
    <td>{{client.lastname}}</td>
    <td>{{client.inumber}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

Chr*_*all 32

你可以使用ng-click(而不是onClick),正如Jason所建议的那样.

就像是:

HTML

<tr ng-repeat="client in clients" ng-click="showClient(client)">
    <td><a ng-href="#/user/{{client.tagid}}">{{client.firstname}}</a></td>
    <td>{{client.lastname}}</td>
    <td>{{client.inumber}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

调节器

$scope.showClient = function(client) {
  $location.path('#/user/' + client.tagid);
};
Run Code Online (Sandbox Code Playgroud)

和样式使它显示为一个可点击的元素(不会在IE7中工作)

CSS

tr {
  cursor: pointer;
}
// or
[ng-click] {
  cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)

  • Angular.js有一个特殊的处理程序:http://docs.angularjs.org/api/ng.directive:ngClick (3认同)
  • 在控制器中,您还应该使用$ location服务和$ location.path('/ user /'+ client.tagid).这将允许您使用html5模式和hashbangs前缀网址.此外,如果您真的想使用窗口,请尝试使用$ window服务,这将为您以后的测试提供灵活性. (2认同)

mir*_*lon 11

我写了一个指令,你可以简单地写:

<tr ng-repeat="client in clients" such-href="#/user/{{client.tagid}}">
Run Code Online (Sandbox Code Playgroud)

来源:

app.directive('suchHref', ['$location', function ($location) {
  return{
    restrict: 'A',
    link: function (scope, element, attr) {
      element.attr('style', 'cursor:pointer');
      element.on('click', function(){
        $location.url(attr.suchHref)
        scope.$apply();
      });
    }
  }
}]);
Run Code Online (Sandbox Code Playgroud)

  • 很有用.我使用`$ location.path()`代替,因为`url()`不起作用.另外,使用`$ location.path()`,url不能包含前导`#`(它必须以`/`开头). (4认同)