1

First time trying to get json data and show it to my app using AngularJS, does not show any data, this is my code implemented

HTML

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>

    <td>{{ x.id }}</td>
    <td>{{ x.title }}</td>

</table>

</div>

</body>
</html>

Script

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://example.com/api/get_page_index/")
    .then(function (response) {$scope.names = response.data.pages;});
});
</script>

Json content from url

 {
      "status": "ok",
      "pages": [
        {
          "id": 2,
          "type": "page",
          "slug": "sample-page",
          "url": "http:\/\/example.com\/",
          "status": "publish",
          "title": "example page",
          "content": "",
          "excerpt": "",
          "date": false,
          "modified": "2016-08-09 17:28:01",
          "comments": [],
          "attachments": [],
          },
3
  • First, are there any errors in your console? Second, what exactly do you want the result to look like? Commented Sep 13, 2016 at 3:07
  • no error on my console, i want display the results from all json file id and title Commented Sep 13, 2016 at 3:24
  • @Max did you check the answer? Commented Sep 13, 2016 at 4:04

1 Answer 1

1

You can do this,

 <table>
    <thead>
      <tr>
        <th ng-repeat="(header, value) in results[0]">
          {{header}}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="row in results">
        <td ng-repeat="cell in row">
          {{cell}}
        </td>
      </tr>
    </tbody>
  </table>

WORKING DEMO

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

3 Comments

it works, but It returns all the result of json. I just want the result of the title and the id from json
Hi Sajeetharan, yes it works thanks!!! i have a questions if want to access to "author" from json and its children elements "name" what would be the right way? ` "author": { "id": 1, "name": "admin", "first_name": "", "last_name": "", "url": "", "description": "" }, `
"pages": [ { "id": 2, "author": { "id": 1, "name": "admin", "first_name": "", "last_name": "", "url": "", "description": "" },

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.