AngularJS provides $http control, which can read data from the server as a service. The server can make a database call to get the records. AngularJS requires data in JSON format. Once the data is ready, $http can be used to get the data from the server in the following way.
function studentController($scope,$http) { var url="data.txt"; $http.get(url).success( function(response) { $scope.students = response; }); }
Here, the student record contained in data.txt. The $http service makes Ajax calls and sets properties specific to its students. The "student" model can be used to draw HTML tables.
Example
data.txt
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).success( function(response) { $scope.students = response; }); } </script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> </body> </html>
Output
To run this example, you need to deploy textAngularJS.html, data.txt to a web server. Open textAngularJS.html in a web browser using the URL to request the server. See the results as follows: