Search code examples
angularjshttpgetdata-retrieval

How to access data from JSON file though Angularjs $http get req?


15.html

       <html>
       <meta charset="utf-8">
     <title>Angularjs htttp services</title>
      <script  src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js">                 

   </script>
   <script src="controller15.js"></script>
   </head>
    <body ng-app="mainApp">
      <div ng-controller="people">
      <ul>
  <h2> names and ages of the programmers</h2>
   <li ng-repeat="person in persons">
    {{ person.Name +':' +person.Age}}
   </li>
   </ul>
   </div>
  </body>
  </html>   

controller15.js

       var app=angular.module('mainApp',[]);
      app.controller('people', function($scope,$http)   
    {
     $http.get('database.json')
     .success(function(response)
    {
      $scope.person=response.records;
   });
   });

database.json where I stored the info:

      {
      "records":[
       {
       "Name":"mehul","Age":"13"
       },
       {
       "Name":"ashok","Age":"23"
       },
       {
        "Name":"rajesh","Age":"44"
       }
       ]

 }

I have attached all of the code, please review my code. I am a beginner in angularjs; it would be always good if you suggest me a particular material or web site to go through.


Solution

  • First: Do not use a minified version of angular to see described errors in your browser console. then tel me what errors do you see!

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