AngularJS – Ajax
AngularJS, sunucudaki verileri okumak için bir servis olarak çalışan $ https: kontrol sağlar. Sunucu istenen kayıtları almak için bir veritabanı çağrısı yapar. AngularJS, JSON formatında veri alır. Veriler hazır olduğunda, $ https: sunucudan verileri aşağıdaki şekilde elde etmek için kullanılabilir:
1
2
3
4
5
6
7
|
function studentController($scope,$https:) {
var url = "data.txt";
$https:.get(url).success( function(response) {
$scope.students = response;
});
}
|
Burada, data.txt dosyası öğrenci kayıtlarını içerir. $ Https: servis bir ajax çağrısı yapar ve öğrencilerin özelliklerini yanıt verir. Öğrenci modeli HTML tablolar çizmek için kullanılabilir.
Örnekler
data.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
[
{
"Name" : "Mahesh Parashar",
"RollNo" : 101,
"Percentage" : "80%"
},
{
"Name" : "Dinkar Kad",
"RollNo" : 201,
"Percentage" : "70%"
},
{
"Name" : "Robert",
"RollNo" : 191,
"Percentage" : "75%"
},
{
"Name" : "Julian Joe",
"RollNo" : 111,
"Percentage" : "77%"
}
]
|
testAngularJS.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<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>
|
Çıktı
Bu örneği uygulamak için testAngularJS.htm ve data.txt dosyalarını bir web sunucusunda çalıştırmanız gerekir. Bir web tarayıcısında sunucunuzun URL’sini kullanarak testAngularJS.htm dosyasını açıp, sonucu görebilirsiniz .
isim | Numara | Yüzde |
---|---|---|
Mahesh Parashar | 101 | % 80 |
Dinkar Kad | 201 | % 70 |
Robert | 191 | % 75 |
Julian Joe | 111 | % 77 |
Leave a reply