将正常的js变量绑定为角度对象键

时间:2014-05-15 15:34:16

标签: angularjs angularjs-scope angularjs-ng-repeat

我有一个对象数组objects,还有一些包含静态字符串的JS变量,例如static1static2 ...我怎样才能完成以下任务:

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

编辑static1static2是JS变量,即。 var static1 = "something"

2 个答案:

答案 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';

它应该正确评估。