我有一个对象数组objects
,还有一些包含静态字符串的JS变量,例如static1
,static2
...我怎样才能完成以下任务:
<tr ng-repeat="obj in objects">
<td>{{obj[static1]}}</td>
<td>{{obj[static2]}}</td>
...
</tr>
编辑:static1
和static2
是JS变量,即。 var static1 = "something"
答案 0 :(得分:1)
你在寻找像这样的东西
<强> Working Code 强>
<强> HTML 强>
<div ng-app='myApp' ng-controller="ArrayController">
<table border="1">
<th ng-repeat="header in headers"> <b>{{ headers[$index]}}</b></th>
<tr ng-repeat="arr in records">
<td ng-repeat="val in arr" ng-bind-html-unsafe="arr[headers[$index]]">
</td>
</tr>
</table>
</div>
<强>脚本强>
var app = angular.module('myApp', []);
app.controller('ArrayController', function ($scope) {
$scope.headers = ['static1', 'static2'];
$scope.records = [{
static1: 'a1',
static2: 'd1'
}, {
static1: 'c2',
static2: 'A2'
}, {
static1: 'b3',
col2: 'c3'
}, {
static1: 'd4',
static2: 'a1'
}, {
static1: '11',
static2: '22'
}, {
static1: 'E1',
static2: 'E2'
}];
});
答案 1 :(得分:1)
在您定义$scope.objects
的控制器中,还要定义:
$scope.static1 = 'nameOfObjectProperty1';
$scope.static2 = 'nameOfObjectProperty2';
它应该正确评估。