Search code examples
angularjsangularjs-ng-repeat

Nested interpolation


I return an array of people from the database eg,

[
{
    id: 1,
    name: 'Jim',
    address: "123 Test Street",
    phone: "999999999"
},
{
    id: 2,
    name: 'Tom',
    address: "123 Test Street",
    phone: "888888888"
},
{
    id: 3,
    name: 'Harry',
    address: "123 Test Street",
    phone: "012345678"
}
]

my API allows me to select a partial person by setting the fields parameter

eg for this example,

&fields=id,name,address,phone

full url for this example, ?q=&fields=id,name,address,phone&id=1,2,3

I want to be able to dynamically generate a table based on the fields selected.

Something like this,

<table>
    <thead>
        <tr>
            <th ng-repeat="field in fields">[[ field.text ]]</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="person in people">
            <td ng-repeat="field in fields">[[ person.[[ field.id ]] ]]</td>
        </tr>
    </tbody>
</table>

How can I interpolate the field.id so i can use to it select the key in person.

Edit, I have changed the interpolation characters to [[ & ]].


Solution

  • This line

    <td ng-repeat="field in fields">[[ person.[[ field.id ]] ]]</td>
    

    should be

    <td ng-repeat="field in fields">[[ person[field.id] ]]</td>