0

Following this tutorial: http://www.w3schools.com/angular/angular_http.asp i can parse a json perfectly but i've got a problem.. I have a php with a json in which inside there is a json something like this:

{"item":[ 
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"City" : "Luleå",
"Country" : "Sweden"
}]}

As you can see there is a JSONObject (item).. actually to parse a json without JSONObject i do this function inside my controller:

$http.get(JSONUrl)
        .success(function(response) {
            $scope.names = response;
        });

and then

<div data-ng-controller="MainController">
                <table class="uk-table uk-table-striped uk-table-hover" data-ng-if="!loading && !error">
                    <thead>
                    <th>Nome</th>
                    <th>Ver</th>
                    <th>Code</th>
                    </thead>
                    <tbody>
                    <tr data-ng-repeat="item in names">
                        <td>{{item.Name}}</td>
                        <td>{{item.City}}</td>
                        <td>{{item.Country}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>

But in my case i display nothing becase i have that "item" as JSONObject.. Any solution?

1 Answer 1

3

Your problem is you are not using correct property in response JSON Object

Use

<tr data-ng-repeat="item in names.item">

instead of

<tr data-ng-repeat="item in names">

OR

$scope.names = response.item;
Sign up to request clarification or add additional context in comments.

3 Comments

Amazing it works!! there is no documentation about that right? Thank you
Sure, the documentation for using ng-repeat should cover it. To be clear, the only change he made was he referenced the property that was an array, rather than the object as a whole. You won't find documentation that explains that, that's javascript 101. If you want to repeat on name.item, use name.item.
Oh thanks, i'm a little bit rusty with javascript :)

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.