首頁 > web前端 > js教程 > 主體

AngularJS Ajax實例詳解

怪我咯
發布: 2017-06-27 11:51:19
原創
1297 人瀏覽過

本文主要講解AngularJS Ajax的知識,這裡提供詳細資料及程式碼範例,幫助學習AngularJS的朋友,有需要的小夥伴可以參考下

AngularJS提供$http控制,可以作為一項服務從伺服器讀取資料。伺服器可以使一個資料庫呼叫來取得記錄。 AngularJS需要JSON格式的資料。一旦資料準備好,$http可以用以下面的方式從伺服器取得資料。

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>
<p 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>
</p>
<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在網頁瀏覽器中開啟textAngularJS.html請求伺服器。看到結果如下:

#

以上是AngularJS Ajax實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!