在AngularJS中使用AJAX的方法
AngularJS提供$http控制,可以作為一項服務(wù)從服務(wù)器讀取數(shù)據(jù)。服務(wù)器可以使一個數(shù)據(jù)庫調(diào)用來獲取記錄。 AngularJS需要JSON格式的數(shù)據(jù)。一旦數(shù)據(jù)準備好,$http可以用以下面的方式從服務(wù)器得到數(shù)據(jù)。
function studentController($scope,$http) { var url="data.txt"; $http.get(url).success( function(response) { $scope.students = response; }); }
在這里,data.txt中包含的學(xué)生記錄。 $http服務(wù)使Ajax調(diào)用和設(shè)置針對其學(xué)生的屬性。 “學(xué)生”模型可以用來用來繪制 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到一個網(wǎng)絡(luò)服務(wù)器。使用URL在Web瀏覽器中打開textAngularJS.html請求服務(wù)器??吹浇Y(jié)果如下:
- AngularJs ng-repeat 嵌套如何獲取外層$index
- AngularJS入門(用ng-repeat指令實現(xiàn)循環(huán)輸出
- AngularJS ng-repeat數(shù)組有重復(fù)值的解決方法
- Angularjs的ng-repeat中去除重復(fù)數(shù)據(jù)的方法
- AngularJS使用ng-repeat指令實現(xiàn)下拉框
- AngularJS基礎(chǔ) ng-repeat 指令簡單示例
- Angularjs中ng-repeat-start與ng-repeat-end的用法實例介紹
- AngularJS實現(xiàn)ajax請求的方法
- AngularJS入門教程之與服務(wù)器(Ajax)交互操作示例【附完整demo源碼下載】
- 實例詳解angularjs和ajax的結(jié)合使用
- AngularJS ng-repeat指令及Ajax的應(yīng)用實例分析
相關(guān)文章
AngularJS模態(tài)框模板ngDialog的使用詳解
這篇文章主要介紹了AngularJS模態(tài)框模板ngDialog的使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05利用VS Code開發(fā)你的第一個AngularJS 2應(yīng)用程序
這篇文章主要給大家介紹了關(guān)于利用VS Code如何開發(fā)你的第一個AngularJS 2應(yīng)用程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友下面來一起看看吧。2017-12-12AngularJS實現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等功能實例
這篇文章給大家分享了AngularJS循環(huán)實現(xiàn)數(shù)據(jù)列表的增加、刪除和上移下移等基礎(chǔ)功能,對大家學(xué)習(xí)AngularJS具有一定的參考借鑒價值,有需要的朋友可以看看。2016-09-09Angular.js中$resource高大上的數(shù)據(jù)交互詳解
這篇文章主要給大家介紹了關(guān)于Angular.js中$resource高大上的數(shù)據(jù)交互的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用angular.js具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起看看吧。2017-07-07angular6的table組件開發(fā)的實現(xiàn)示例
這篇文章主要介紹了angular6的table組件開發(fā)的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12