0

I'm having trouble trying to dynamically display all o the json values in angular using ng-repat

here what what I'am hoping to replacate using hard coded values

html

 <div ng-repeat="user in users">
            {{user.userId}} {{user.userName}} {{user.password}}
        </div>

output

3 DAve password
4 bob password
4 kjdjkdj
1 names password2

I'm trying to write the angular repeat to be more generic so I can pass i any amount of json gather from a mongoose model. So far I've gotten

<div ng-repeat="(key, value) in users">
        <td> {{key}} </td> <td> {{ value }} </td>
        </div>

With this loop I'm getting too much information with an output that follows

0 {"_id":"527a732fe5f8dabf5b00020c","userId":3,"userName":"DAve","password":"password"}
1 {"userId":4,"userName":"bob","password":"password","_id":"527abf6ebaa5eb8426000001","__v":0}
2 {"userId":4,"password":"kjdjkdj","_id":"527d62fc85612ab42b000001","__v":0}
3 {"__v":0,"_id":"528130182c5544b81f000002","password":"password2","userId":1,"userName":"names"}

how can i modify the following loop to output only the userName password and userId without having to explicitly declare them like in my first loop.

5
  • I assume users is a collection, so there are no keys, just indices. Commented Nov 11, 2013 at 22:55
  • 1
    So, you want something generic, but which only displays userName, password, and userId? Isn't that completely contradictory? Commented Nov 11, 2013 at 22:55
  • users is a collection Commented Nov 11, 2013 at 22:56
  • 1
    I agree with @JBNizet, what you want to do is an unnecessary abstraction. If you want to have a generic list of things, create a directive. Commented Nov 11, 2013 at 23:00
  • Sweet thanks I'll start looking at the angular documentation for directives. Commented Nov 11, 2013 at 23:03

1 Answer 1

1

If you want to print all values, within user without knowing what or how many there are ahead of time you could use a second ng-repeat to iterate over the key value pairs of each object as such:

<div ng-repeat="user in users">
    <div ng-repeat="(k, v) in user">
          {{v}} 
    </div>
</div>

This only prints the values since it looks like that's the part you're interested in. Clearly you could print both.

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

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.