使用 ng-repeat 时设置 tr 的 id
setting id of tr while using ng-repeat
我有以下代码用于生成 table 和 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 link 可以正常工作:
现在的问题是我想假设 tr 的 id 如下:id="tr"+x.name
例如:id="trlemon"
但我不知道如何设置它。如果我用 jquery 制作这个 table 我可以很容易地做到这一点,但现在用 angularjs 似乎有点棘手有人能帮忙吗?
您可以只使用典型的 Angular 表达式 {{..}}
:
<tr id="tr{{x.name}}" ng-repeat="x in typesHash ">
</tr>
我有以下代码用于生成 table 和 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 link 可以正常工作:
现在的问题是我想假设 tr 的 id 如下:id="tr"+x.name 例如:id="trlemon" 但我不知道如何设置它。如果我用 jquery 制作这个 table 我可以很容易地做到这一点,但现在用 angularjs 似乎有点棘手有人能帮忙吗?
您可以只使用典型的 Angular 表达式 {{..}}
:
<tr id="tr{{x.name}}" ng-repeat="x in typesHash ">
</tr>