JohnnyTrous
JohnnyTrous

Reputation: 43

AngularJS ng-repeat get data from JSON

I got a problem on getting data from ng-repeat.

<tr ng-repeat="x in records">
    <td>{{x.id}}</td>
    <td>{{x.title}}</td>
    <td>{{x.year}}</td>
    <td>{{x.note}}</td>
</tr>

The {{x.id}} is the tt0013158 I want to get from the JSON.

Here is the JSON:

{"result":{
    "tt0013158":{
        "note":"",
        "title":"The Frozen North",
        "year":"1922"
    },
    "tt1605783":{
        "note":"",
        "title":"Midnight in Paris",
         "year":"2011"
    }
}}

I can get the note, title and year correctly. But how can I get the id (such as tt0013158) from the JSON?

Upvotes: 2

Views: 71

Answers (3)

AlainIb
AlainIb

Reputation: 4728

you need to use (key,value) syntax :

<tr ng-repeat="(key, value) in data in records.result">
    <td>{{key}}</td>
    <td>{{value.id}}</td>
    <td>{{value.title}}</td>
    <td>{{value.year}}</td>
    <td>{{value.note}}</td>
</tr>

Upvotes: 1

zeeshan Qurban
zeeshan Qurban

Reputation: 387

You can use following approach to iterate through ng-repeat

<tr ng-repeat="item in records">
<td>{{item.id}}</td>
<td>{{item.note}}</td>
<td>{{item.title}}</td>
<td>{{item.year}}</td>
</tr>

Upvotes: 0

Seva
Seva

Reputation: 2488

There is no notion in javascript (or angular) that tt0013158 in the sample above would be the object's id. It's just a key in a map.

I believe this should do the trick:

<tr ng-repeat="(key, x) in records.result">
    <td>{{key}}</td>
    <td>{{x.title}}</td>
    <td>{{x.year}}</td>
    <td>{{x.note}}</td>
</tr>

Upvotes: 3

Related Questions