Fahad Khan
Fahad Khan

Reputation: 1635

JSON object not being accessed using AngularJS

I am just unable to access values from the my json string (image below).

{
    all: [{
        info: {
            name: "Fahad",
            lat: "41.954815",
            lon: "-87.647209"
        }
    }, {
        info: {
            name: "Fahad",
            lat: "41.954815",
            lon: "-87.647209"
        }
    }, {
        info: {
            name: "Fahad",
            lat: "41.954815",
            lon: "-87.647209"
        }
    }],
    rowsfound: "11"
}

I am using angularjs, here is html

<ul ng-controller="PostsCtrl">
    <li ng-repeat="post in posts">
        {{post.all[0].info.name}}  <em>{{post.rowsfound}}</em>
    </li>
</ul>

Controller:

app.controller("PostsCtrl", function ($scope, $http) {
    $http.get(myurl)
        .success(function (data) {
        $scope.posts = data;

    }).error(function (data, status, headers, config) {
        alert("error");
    });
});

two list items generated without any value. whats the wrong thing am I doing?

Thanks

Upvotes: 2

Views: 78

Answers (2)

Entomo
Entomo

Reputation: 275

Your JSON representation is wrong, correct is "name": "Fahad" etc. You can use tools like http://jsonlint.com/ to check if your JSON is valid.

Upvotes: 0

Mukund Kumar
Mukund Kumar

Reputation: 23191

use this html:

<ul ng-controller="PostsCtrl">
    <li ng-repeat="post in posts.all">
        {{post.info.name}}  <em>{{posts.rowsfound}}</em>
    </li>
</ul>

Upvotes: 3

Related Questions