4

I want an Angular list showing all the instances of one object property from an array of objects, and nothing more – for example, only the countries.

$scope.testSites = [
                { "site": "Testsite1", "country": "Country1", "customer": "Customer1"},
                { "site": "Testsite2", "country": "Country2", "customer": "Customer2"}
            ];
$scope.chosenCategory = 1;
$scope.categoryNames = ["site", "country", "customer"];
$scope.aspect = $scope.categoryNames[$scope.chosenCategory];

However, I want to use the above variable 'aspect' for choosing which property to show in the list. Something like {{x.country}}, though it works, is therefore not sufficient. I've tried this, but it returns an empty list:

<table border="1" class="list-group-item list-group-item-success">
            <tr>
                <th>{{aspect | capitalize}}</th> 
            </tr>

            <tr ng-repeat="x in testSites | orderBy:myOrderBy">
                <td>
                    {{x.aspect}}
                </td>

            </tr>
        </table>

Is there something I can do?

1 Answer 1

6

You can do via {{ x[aspect] }} - [] bracket notation. It evaluates the expressoin and uses the result to find the property.

You can find Demo here

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

3 Comments

Thanks for the demo. Can I put it into the answer?
Omg, I can't believe it was that simple! You're the hero, thank you! :)
@Zooly Thanks a lot

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.