0

I'm trying to getting items._id value in ng view by using ng-repeat. occurring all data but i want specific data.

data.json

 [ { _id        : "td6v9db4514cc4ewew4334",
     firstName  : 'ayaz',
     lastName   : 'memon',
     items      : '[{"_id":"item2","_name":"My Item #4"},
                    {"_id":"item3","_name":"My Item #4"}]',
     totalItems :  3,
     totalPrice :  2999.97 } ]

Controller

app.controller('myCtrl', function($scope, $http) {
  $http.get("data.json").then((response) => {
    console.log(response.data)

    $scope.userInfo = response.data
  })
})

ng view

<body ng-app="myApp">

<div ng-controller="myCtrl">
<ul ng-repeat="x in userInfo">
  <li >{{x}}</li>

</ul>

5
  • 1
    ng-repeat="item in x.items" will return each item Commented Aug 7, 2017 at 6:35
  • 1
    You will have to use nested ng-repeat. Commented Aug 7, 2017 at 6:35
  • The data you show is not valid JSON. Keys in JSON have to be surrounded by ". Commented Aug 7, 2017 at 6:36
  • Possible duplicate of How to use ng-repeat for dictionaries in AngularJs? Commented Aug 7, 2017 at 6:38
  • check out this plnkr.co/edit/6jTrwMKdLDcH5T3ZFiS7?p=preview Commented Aug 7, 2017 at 13:21

5 Answers 5

1

Here you are using nested json object ie items in userInfo, you can write ng-repeat as,

<body ng-app="myApp">
<div ng-controller="myCtrl">
<ul>
  <li ng-repeat="x in userInfo.items">{{x._id}}</li>
</ul>

Note: It will be good to understand if you use ng-repeat in <li></li> instead of <ul></ul>

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

5 Comments

Just a minor problem, userInfo is an array of object and not object.
Yes I'm agree with you, but its single element in array so we can use like userInfo[0]
Or its just a sample input and actual data consist of n objects.
<ul> <li ng-repeat="x in userInfo.items">{{x._id}}</li> </ul> It's not working, if i remove .items then it's giving me specific output, but not giving me items result
Try this, <ul ng-repeat="x in userInfo"> <li ng-repeat="y in x" > <ul> <li ng-repeat="item in y.items">{{item._id}}</li> </ul> </li> </ul>
0

Try using nested ng-repeat like this:

<ul ng-repeat="user in userInfo">
  <li ng-repeat="x in user.items">{{x._id}} : {{x._name}}</li>
</ul>

I'm assuming your http call will return a valid response because the Json data you've given is invalid. The keys need to be in enclosed within " ". I've also structured the the ng-repeat assuming that your response will have multiple objects

1 Comment

there is nothing appearing
0

Try this,

   [{
    "_id": "td6v9db4514cc4ewew4334",
    "firstName": "ayaz",
    "lastName": "memon",
    "items": [{
            "_id": "item2",
            "_name": "My Item #4"
        },
        {
            "_id": "item3",
            "_name": "My Item #4"
        }
    ],
    "totalItems": 3,
    "totalPrice": 2999.97
}]

and you could get your repeated "_id" in the items now in ng-repeat="item in userInfo.items" and with {{item._id}} .

Please correct me if there's any mistake, I'm still new. Thanks.

4 Comments

How is this: "items" : {"_id":"item2","_name":"My Item #4"}, {"_id":"item3","_name":"My Item #4"}, a valid JSON?
@Rajesh you're right, I overlooked it. Thanks for correcting me.
When you change something in your answer, please check if rest of it actually is in sync with update. Pointing to this is not a nested JSON.
@NomanKt Like I mentioned earlier, it was your JSON format, check this out plnkr.co/edit/eb1LCy0YqPDFlwXLci4c?p=preview , I just changed your JSON.
0

I was trying to retrieve string as an object that was a mistake. I used object data instead of string and got result.

Comments

0

This is worked for me:

<ul>
  <li ng-repeat="user in userInfo">
     <ul>
       {{user._id}}
       <li ng-repeat="item in user.items">
         {{item._id}}
       </li>
     </ul>
  </li>
</ul>

Plunker Example: http://plnkr.co/edit/hzWdj2IiUI2RytYVUI7m?p=preview

2 Comments

Something wrong with your JSON Data, coz as you can see in my Plunker example I have used the same object as yours and it worked.
have you found solution?

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.