0

Im learnign angularJs, and i want to import an array from a json on my controller Like that:

myApp.controller("demoCtrl", function ($scope, $http) {

            var promise = $http.get("todo.json");

            promise.then(function (data) {
                $scope.todos = data;
            });


        });

and im using a table to display the data on todos:

<table class="table">
            <tr>
                <td>Action</td>
                <td>Done</td>
            </tr>
            <tr ng-repeat="item in todos">
                <td>{{item.action}}</td>
                <td>{{item.done}}</td>
            </tr>
        </table>

and this results on the flowing html page:

enter image description here

<!DOCTYPE html>
<html ng-app="demo">

<head>
    <title>Example</title>
    <link href="../css/bootstrap.css" rel="stylesheet" />
    <link href="../css/bootstrap-theme.css" rel="stylesheet" />
    <script src="angular.js"></script>
    <script type="text/javascript">

        var myApp = angular.module("demo", []);

        myApp.controller("demoCtrl", function ($scope, $http) {

            var promise = $http.get("todo.json");

            promise.then(function (data) {
                $scope.todos = data;
            });



        });
    </script>
</head>

<body ng-controller="demoCtrl">
    <div class="panel">
        <h1>To Do</h1>
        <table class="table">
            <tr>
                <td>Action</td>
                <td>Done</td>
            </tr>
            <tr ng-repeat="item in todos">
                <td>{{item.action}}</td>
                <td>{{item.done}}</td>
            </tr>
        </table>
    </div>
</body>

4
  • Are there errors on the console (browser)? are 'ng-app', 'ng-controller' there in the html body? Commented Apr 9, 2020 at 23:33
  • @Mostav I notice that i forgot the ng-app, but I fixed that, and now just show empty <td> tags Commented Apr 10, 2020 at 0:14
  • Can you post the whole html and js file if possible? question without all necessary elements will not help at all. Commented Apr 10, 2020 at 0:15
  • 1
    @Mostav I've edited the post Commented Apr 10, 2020 at 0:34

2 Answers 2

1

The normal way of getting access to the json is from the data within the returned object from the http request - you are tying to use the entire returned object.

I use "response" as the return from the get request - then the data is "response.data". This is needed because there are other properties returned within the response object from the get request.

Try changing your promise to be as follows:

promise.then(function (response) {
   $scope.todos = response.data;
});

Also you should be having a thead and th's and tbody in the table to show a more semantically correct table

<table class="table">
   <thead>
     <tr>
       <th scope="col">Action</th>
       <th scope="col">Done</th>
      </tr>
   </thead>
   <tbody>
      <tr ng-repeat="item in todos">
          <td>{{item.action}}</td>
          <td>{{item.done}}</td>
       </tr>
    </tbody>
   </table>
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help - if you can mark the answer as accepted so that other devs know the issues has been resolved. Happy coding
0

Promise return entire response in callback Data is in response.data

myApp.controller("demoCtrl", function ($scope, $http) {
  var promise = $http.get("todo.json");
  // Entire response in callback
  promise.then(function (response) {
    $scope.todos = response.data; // Data is in response.data
  });
});

More: https://docs.angularjs.org/api/ng/service/$http

Comments

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.