我有一个使用 angularjs 的表,我想在其中循环遍历数组以打印来自 json 对象的特定 header 。标题打印得很好,但是当我尝试使用嵌套的 ng-repeat 中的变量作为 json 选择器时,问题就来了。如果您将内部的 ng-repeat 替换为下面的注释部分,它将起作用。
表:
<table>
<thead>
<th ng-repeat="column in tableHeader">{{column}} <a ng-click="sort_by(column);"><i class="glyphicon glyphicon-sort"></i></a></th>
</thead>
<tbody>
<tr ng-repeat="data in filtered>
<td ng-repeat="column2 in tableHeader">{{data.column2}}</td>
<!-- <td>{{data.Environment}}</td>
<td>{{data.HostIP}}</td>
<td>{{data.ServiceName}}</td>
<td>{{data.Status}}</td>
<td>{{data.StartTime}}</td>
<td>{{data.Capacity}}</td>
<td>{{data.Txn}}</td>
<td>{{data.Errors}}</td>
<td>{{data.Build}}</td>
<td>{{data.Project}}</td>
<td>{{data.Author}}</td>
<td>{{data.ModifyDate}}</td>
<td>{{data.Port}}</td>
<td>{{data.BasePath}}</td> -->
</tr>
</tbody>
</table>
位于 Controller 中的数组:
$scope.tableHeader = ['Environment', 'HostIP', 'Status', 'ServiceName', 'StartTime', 'Capacity', 'Txn', 'Errors', 'Build', 'Project', 'Author', 'ModifyDate', 'Port', 'BasePath'];
最佳答案
我认为您正在寻找 {{data[column2]}}
。由于 column2
只是您想要的属性的字符串值,因此在这种情况下将 data
视为关联数组以获取您要显示的属性。
关于javascript - AngularJS - 在 JSON 选择器中使用变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29527552/