??????? $http是AngularJS 中的一個核心服務,用于讀取遠程服務器的數據。
??????? 以下是存儲在web服務器上的 JSON 文件data.json。
[
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Berglunds snabbk?p",
"City" : "Lule?",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"City" : "México D.F.",
"Country" : "Mexico"
},
{
"Name" : "Ernst Handel",
"City" : "Graz",
"Country" : "Austria"
},
{
"Name" : "FISSA Fabrica Inter. Salchichas S.A.",
"City" : "Madrid",
"Country" : "Spain"
},
{
"Name" : "Galería del gastrónomo",
"City" : "Barcelona",
"Country" : "Spain"
},
{
"Name" : "Island Trading",
"City" : "Cowes",
"Country" : "UK"
},
{
"Name" : "K?niglich Essen",
"City" : "Brandenburg",
"Country" : "Germany"
},
{
"Name" : "Laughing Bacchus Wine Cellars",
"City" : "Vancouver",
"Country" : "Canada"
},
{
"Name" : "Magazzini Alimentari Riuniti",
"City" : "Bergamo",
"Country" : "Italy"
},
{
"Name" : "North/South",
"City" : "London",
"Country" : "UK"
},
{
"Name" : "Paris spécialités",
"City" : "Paris",
"Country" : "France"
},
{
"Name" : "Rattlesnake Canyon Grocery",
"City" : "Albuquerque",
"Country" : "USA"
},
{
"Name" : "Simons bistro",
"City" : "K?benhavn",
"Country" : "Denmark"
},
{
"Name" : "The Big Cheese",
"City" : "Portland",
"Country" : "USA"
},
{
"Name" : "Vaffeljernet",
"City" : "?rhus",
"Country" : "Denmark"
},
{
"Name" : "Wolski Zajazd",
"City" : "Warszawa",
"Country" : "Poland"
}
]
??????? AngularJS $http是一個用于讀取web服務器上數據的服務。$http.get(url) 是用于讀取服務器數據的函數。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8"></meta>
<title>AngularJS XMLHttpRequest</title>
<script src="angular/angular.js"></script>
</head>
<body>
<div ng-app="" ng-controller="customersController">
<ul>
<li ng-repeat="x in names">{{ x.Name + ', ' + x.Country }}</li>
</ul>
</div>
</body>
<script>
function customersController($scope,$http) {
$http.get("data.json").success(function(response) {$scope.names = response;});
//$http.get("http://localhost:8088/angularjs-http/data.json").success(function(response) {$scope.names = response;});
}
</script>
</html>
應用解析
:
1.AngularJS 應用通過 ng-app 定義。應用在 <div> 中執行。
2.ng-controller 指令設置了controller 對象名。
3.函數 customersController 是一個標準的 JavaScript 對象構造器。
4.控制器對象有一個屬性: $scope.names。
5.$http.get() 從web服務器上讀取靜態 JSON 數據。
6.當從服務端載入 JSON 數據時,$scope.names 變為一個數組。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

