We now have a youtube channel. Subscribe!

AngularJS | Ajax

AngularJS | Ajax


Hello folks! welcome back to a new edition of our tutorial on AngularJS. In this tutorial, we will be studying about AngularJS Ajax.

AngularJS gives $http control which works as a service to read data from server. The server calls the database to get the desired records. AngularJS needs data in format of JSON. Once the data is ready, $http control can be used to obtain the data from server in the following way -

function studentController($scope,$https:) {
   var url = "data.txt";

   $https:.get(url).success( function(response) {
      $scope.students = response; 
   });
}

Here, the file data.txt holds student records. $http service makes an AJAX call and sets response to its property students. students model can be used to draw tables in HTML.

Example

Create a file data.txt.

[
   {
      "Name" : "Kennedy Nkpara",
      "RollNo" : 101,
      "Percentage" : "80%"
   },
   {
      "Name" : "Bethel Igwela",
      "RollNo" : 201,
      "Percentage" : "70%"
   },
   {
      "Name" : "Stephanie Francis",
      "RollNo" : 191,
      "Percentage" : "75%"
   },
   {
      "Name" : "Julian Joe",
      "RollNo" : 111,
      "Percentage" : "77%"
   }
]


Create a file testAngularJS.html.

<html>
   <head>
      <title>Angular JS Includes</title>
      
      <style>
         table, th , td {
            border: 1px solid grey;
            border-collapse: collapse;
            padding: 5px;
         }
         table tr:nth-child(odd) {
            background-color: #f2f2f2;
         }
         table tr:nth-child(even) {
            background-color: #ffffff;
         }
      </style>
   </head>
   
   <body>
      <h2>AngularJS Sample Application</h2>
      <div ng-app = "" ng-controller = "studentController">
      
         <table>
            <tr>
               <th>Name</th>
               <th>Roll No</th>
               <th>Percentage</th>
            </tr>
         
            <tr ng-repeat = "student in students">
               <td>{{ student.Name }}</td>
               <td>{{ student.RollNo }}</td>
               <td>{{ student.Percentage }}</td>
            </tr>
         </table>
      </div>
      
      <script>
         function studentController($scope,$http) {
            var url = "/data.txt";

            $http.get(url).then( function(response) {
               $scope.students = response.data;
            });
         }
      </script>
      
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">
      </script>
      
   </body>
</html>

Output

To execute the above example, you need to deploy testAngularJS.html and data.txt file to a server. Open the file testAngularJS.html using the URL of your server in a browser to see the following result -

AngularJS Sample Application

NameRoll NoPercentage
Kennedy Nkpara10180%
Bethel Igwela20170%
Stephanie Francis19175%
Julian Joe11177%


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we are going to be studying about AngularJS Views.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain