1

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"

1
  • static1 will be json key right Commented May 15, 2014 at 15:39

2 Answers 2

1

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

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

and it should evaluate correctly.

Sign up to request clarification or add additional context in comments.

4 Comments

static1 and static2 are normal variables, not angularjs scope variables
everything you put in expressions like obj[static1] has to be on the angular scope. obj is (ng-repeat puts it). It's how angular works.
apparently its the only way to go... I thought there might be a trick or so
will change to any object dynamically reflect? say, obj[static1] is changed on certain onclick function
1

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'
    }];
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.