Вопрос

I have an array of objects, objects, and a a few JS variables holding static strings, such as static1, static2 ... How can I accomplish the following:

<tr ng-repeat="obj in objects">
    <td>{{obj[static1]}}</td> 
    <td>{{obj[static2]}}</td> 
    ...
</tr>

EDIT: static1 and static2 are JS variables, ie. var static1 = "something"

Это было полезно?

Решение

In the controller where you define $scope.objects, also define:

$scope.static1 = 'nameOfObjectProperty1';
$scope.static2 = 'nameOfObjectProperty2';

and it should evaluate correctly.

Другие советы

Are you looking for something like this

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>

script

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'
    }];
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top