ANGULARJS Ajax detailed and sample code _ANGULARJS

Source: Internet
Author: User


ANGULARJS provides $http control and can read data from the server as a service. The server can make a database call to fetch the records. ANGULARJS requires data in JSON format. Once the data is ready, $http can be used to get 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 records included in the Data.txt. $http Service enables Ajax to invoke and set properties for its students. The student model can be used to draw HTML tables.



Example



Data.txt


[
{
' name ': ' Mahesh Parashar ',
' rollno ': '
percentage ': ' 80% '
},
{
' name ': ' Dinkar Kad ",
" Rollno ": 201,
" percentage ":" 70% "
},
{
" Name ":" Robert ",
" Rollno ": 191, "
percentage": "75%"
},
{
"Name": "Julian Joe",
"Rollno": "Percentage": "
77%"
}
]


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 network server. Open the Textangularjs.html request server in a Web browser using the URL. See the results as follows:






The above is ANGULARJS Ajax basic data collation, follow-up to continue to collate relevant information, thank you for your support of this site!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.