在AngularJS中使用AJAX的方法
AngularJS提供$http控制,可以作為一項服務從服務器讀取數(shù)據(jù)。服務器可以使一個數(shù)據(jù)庫調用來獲取記錄。 AngularJS需要JSON格式的數(shù)據(jù)。一旦數(shù)據(jù)準備好,$http可以用以下面的方式從服務器得到數(shù)據(jù)。
function studentController($scope,$http) {
var url="data.txt";
$http.get(url).success( function(response) {
$scope.students = response;
});
}
在這里,data.txt中包含的學生記錄。 $http服務使Ajax調用和設置針對其學生的屬性。 “學生”模型可以用來用來繪制 HTML 表格。
例子
data.txt
{
"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.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>
輸出
要運行這個例子,需要部署textAngularJS.html,data.txt到一個網絡服務器。使用URL在Web瀏覽器中打開textAngularJS.html請求服務器??吹浇Y果如下:

- AngularJs ng-repeat 嵌套如何獲取外層$index
- AngularJS入門(用ng-repeat指令實現(xiàn)循環(huán)輸出
- AngularJS ng-repeat數(shù)組有重復值的解決方法
- Angularjs的ng-repeat中去除重復數(shù)據(jù)的方法
- AngularJS使用ng-repeat指令實現(xiàn)下拉框
- AngularJS基礎 ng-repeat 指令簡單示例
- Angularjs中ng-repeat-start與ng-repeat-end的用法實例介紹
- AngularJS實現(xiàn)ajax請求的方法
- AngularJS入門教程之與服務器(Ajax)交互操作示例【附完整demo源碼下載】
- 實例詳解angularjs和ajax的結合使用
- AngularJS ng-repeat指令及Ajax的應用實例分析
相關文章
對Angular.js Controller如何進行單元測試
這篇文章主要給大家介紹了如何對Angular Controller進行單頁測試。如果你不太了解angular也沒關系,本文也會提及關于Angular的一些知識。文中通過示例代碼介紹的很詳細,詳細對大家的理解和學習很有幫助,下面來一起看看吧。2016-10-10
Angular依賴注入系統(tǒng)里Injection token PLATFORM_ID使用場景詳解
這篇文章主要為大家介紹了Angular依賴注入系統(tǒng)里Injection token PLATFORM_ID使用場景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
AngularJS使用ng-Cloak阻止初始化閃爍問題的方法
這篇文章主要介紹了AngularJS使用ng-Cloak阻止初始化閃爍問題的方法,結合實例形式分析了AngularJS使用ng-Cloak來解決初始化時出現(xiàn)閃爍問題的相關技巧,需要的朋友可以參考下2016-11-11
angular 用攔截器統(tǒng)一處理http請求和響應的方法
下面小編就為大家?guī)硪黄猘ngular 用攔截器統(tǒng)一處理http請求和響應的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

