使用ng-repeat时设置tr的id

时间:2015-01-24 23:25:29

标签: angularjs angularjs-directive angularjs-ng-repeat

我有以下代码用于动态生成带有angularjs的表,工作正常:

<div  class="col-lg-10 col-sm-10 col-xs-10">

     <table class="table table-hover">
<thead>
  <tr>
    <th>item</th>
    <th>price</th>
    <th>number</th>
    <th>edit</th>
  </tr>
</thead>
<tbody ng-controller="table">
<tr  ng-repeat="x in typesHash ">
   <td>{{x.name}}</td>
    <td>{{x.price}}</td>
    <td>{{x.unit}}</td>
    <td>edit</td>
</tr>
  <tr>

  </tr>

</tbody>

这里是js代码:

var app=angular.module('app',[]);
app.controller('table',function($scope){
 $scope.typesHash=[
                 {name : 'lemon', price : 100,unit:2.5 },       
                 {name : 'meat', price : 200,unit:3.3  }];


   });

这里是代码小提琴链接工作正常:

fiddle

现在问题是我想假设tr的id如下:id =&#34; tr&#34; + x.name 例如:id =&#34; trlemon&#34; 但我不知道如何设置它。如果我用jquery生成这个表,我可以很容易地做到这一点,但现在使用angularjs似乎有点棘手,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

您可以使用典型的Angular表达式{{..}}

<tr id="tr{{x.name}}" ng-repeat="x in typesHash ">
</tr>