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ù)器。看到結(jié)果如下:

以上就是AngularJS Ajax的基礎(chǔ)資料整理,后續(xù)繼續(xù)整理相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
詳解在Angularjs中ui-sref和$state.go如何傳遞參數(shù)
這篇文章主要介紹了詳解在Angularjs中ui-sref和$state.go如何傳遞參數(shù),詳細的介紹了ui-sref和$state.go的區(qū)別和如何傳參,有興趣的可以了解下2017-04-04
如何解決手機瀏覽器頁面點擊不跳轉(zhuǎn)瀏覽器雙擊放大網(wǎng)頁
這篇文章主要介紹了如何解決手機瀏覽器頁面點擊不跳轉(zhuǎn)瀏覽器雙擊放大網(wǎng)頁的相關(guān)資料,需要的朋友可以參考下2016-07-07
基于?angular?material?theming?機制修改?mat-toolbar?的背景色(示例詳解
最近在學(xué)習(xí)?angular,記錄一下昨天的進展,解決的問題是通過?theme?的配置修改?mat-toolbar?的背景色,避免對色彩的硬編碼,這篇文章主要介紹了基于?angular?material?theming?機制修改?mat-toolbar?的背景色,需要的朋友可以參考下2022-10-10
AngularJs入門教程之環(huán)境搭建+創(chuàng)建應(yīng)用示例
這篇文章主要介紹了AngularJs入門教程之環(huán)境搭建+創(chuàng)建應(yīng)用的方法,較為詳細的分析了AngularJS的功能、下載及應(yīng)用創(chuàng)建方法,需要的朋友可以參考下2016-11-11
Angular應(yīng)用的多語言設(shè)置問題解決示例
這篇文章主要為大家介紹了Angular應(yīng)用的多語言設(shè)置問題解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07

