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>

Noman Kt
  • 21
  • 8

5 Answers5

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>

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.

C.C.Liew
  • 28
  • 6
  • How is this: `"items" : {"_id":"item2","_name":"My Item #4"}, {"_id":"item3","_name":"My Item #4"},` a valid JSON? – Rajesh Aug 07 '17 at 06:46
  • @Rajesh you're right, I overlooked it. Thanks for correcting me. – C.C.Liew Aug 07 '17 at 06:54
  • 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.* – Rajesh Aug 07 '17 at 07:17
  • @NomanKt Like I mentioned earlier, it was your JSON format, check this out http://plnkr.co/edit/eb1LCy0YqPDFlwXLci4c?p=preview , I just changed your JSON. – C.C.Liew Aug 07 '17 at 15:51
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

kuk_94
  • 483
  • 1
  • 5
  • 18
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

0

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

Noman Kt
  • 21
  • 8