0

I have the following data structure:

{ 
    current: { Number: '8', AnotherNumber: 123 },
}

{ 
    current: { Number: '9', AnotherNumber: 456 },
}

I want in angular view to print out in a table:

Number  AnotherNumber
8       123

I am trying with this, but it is displaying the whole current object

  <li ng-repeat="friend in friends">
        <span ng-repeat="(key, value) in friend">
            {{friend.current}}
        </span>
  </li>

2 Answers 2

1

a single friend element in your case is an object {current:{}} to iterate through keys and values you should do something like:

<li ng-repeat="friend in friends">
    <span ng-repeat="(key, value) in friend.current">
        {{key}} {{value}}
    </span>
</li>

http://plnkr.co/edit/hWKl262YIi0IBhIebJGz?p=preview working plnkr example

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

Comments

1

Your code should look like this:

<table>
    <tr>
        <th>Number</th>
        <th>AnotherNumber</th>
    </tr>
    <tr ng-repeat="friend in friends">
        <td>{{friend.current.Number}}</td>
        <td>{{friend.current.AnotherNumber}}</td>
    </tr>
</table>

Unrelated to the question, but yu can do this for brevity:

<table>
    <tr><th>Number</th><th>AnotherNumber</th></tr>
    <tr ng-repeat="friend in friends" ng-init="c=friend.current">
        <td>{{c.Number}}</td><td>{{c.AnotherNumber}}</td>
    </tr>
</table>

UPDATE:

If you want, you can iterate "blindly" over the keys, but that will result in the columns being sorted alphabetically (unless you add more code to specify their order explicitely):

<table>
    <tr>
        <th ng-repeat="key in firends[0]">{{key}}</th>
    </tr>
    <tr ng-repeat="friend in friends">
        <td ng-repeat="(key value) in friends">{{value}}</td>
    </tr>
</table>

See, also, this short demo.

2 Comments

I should have mentioned that I don't want to access by name, but iterate "blindly", since I am getting that JSON from a schemaless MongoDB.
@SirBenBenji: Yeah, you probably should :) Check out my updated answer for the "catch" in iterating over object keys.

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.