0

I tried to display data from json file with angularJS but i can't arrive to it. I tried many times but i didn't find the probleme. I copied many other examples and i get the same result. Please help me to find it. Here is my html file:

<html>
   <head>
      <title>Angular JS Includes</title>

   </head>
   <body>
      <h2>AngularJS Sample Application</h2>
      <div ng-app = "App" ng-controller = "TodoCtrl">

        <ul>
    <li ng-repeat="todo in todos">
      {{todo.text}} - <em>{{todo.done}}</em>
    </li>
  </ul>
      </div>

      <script>
         var App = angular.module('App', []);

App.controller('TodoCtrl', function($scope, $http) {
  $http.get('todos.json')
       .then(function(res){
          $scope.todos = res.data;                
        });
});
      </script>

      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>

   </body>
</html>

todos.json:

[{ "text":"learn angular", "done":true },
 { "text":"build an angular app", "done":false},
 { "text":"something", "done":false },
 { "text":"another todo", "done":true }]
0

3 Answers 3

1

Add your angular script before the code.

<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>

<script>
  var App = angular.module('App', []);

  App.controller('TodoCtrl', function($scope, $http) {
     $http.get('todos.json')
       .then(function(res){
          $scope.todos = res.data;                
        });
  });
 </script>

Demo

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

Comments

1

You need to add the angular script before your custom script, or use the onload function to let the document load.

<html>
   <head>
      <title>Angular JS Includes</title>
   </head>
   <body>
      <h2>AngularJS Sample Application</h2>
      <div ng-app = "App" ng-controller = "TodoCtrl">

        <ul>
    <li ng-repeat="todo in todos">
      {{todo.text}} - <em>{{todo.done}}</em>
    </li>
  </ul>
      </div>
     <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>

      <script>
         var App = angular.module('App', []);

App.controller('TodoCtrl', function($scope, $http) {
  $http.get("https://api.myjson.com/bins/zx3wn")
       .then(function(res){
          $scope.todos = res.data;                
        });
});
      </script>

   </body>
</html>

2 Comments

This code works, but i how if would like to change another json file? because if i put my angular script before the code i didn't get any result. it's really wreid.
It will not give you any result if you put it after your custom script, because there are no angular functions no angular library to your application. It will just print the raw data. Check the console as well when trying.
0

i just changed the browser to mozilla and it's work. Thank you for your responses.

3 Comments

This is really not the reason why it might work, find the correct reason. If you are loading the json from some other source, check if it is not file:/// load it over a local server.
Yes you are right, i run it with file:/// that's the real reason. Thank you for your help.
Happy to help:) Please mark any of the answers as accepted if it helped. That way the question will be indexed as "answered". Thanks.

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.