AngularJS XMLHttpRequest
$http?是 AngularJS 中的一個(gè)核心服務(wù),用于讀取遠(yuǎn)程服務(wù)器的數(shù)據(jù)。
讀取 JSON 文件
下是存儲(chǔ)在web服務(wù)器上的 JSON 文件:
{"records": [{"Name": "Alfreds Futterkiste","City": "Berlin","Country": "Germany"},{"Name": "Ana Trujillo Emparedados y helados","City": "Mxico D.F.","Country": "Mexico"},{"Name": "Antonio Moreno Taquera","City": "Mxico D.F.","Country": "Mexico"},{"Name": "Around the Horn","City": "London","Country": "UK"},{"Name": "B's Beverages","City": "London","Country": "UK"},{"Name": "Berglunds snabbk枚p","City": "Lule","Country": "Sweden"},{"Name": "Blauer See Delikatessen","City": "Mannheim","Country": "Germany"},{"Name": "Blondel pre et fils","City": "Strasbourg","Country": "France"},{"Name": "Blido Comidas preparadas","City": "Madrid","Country": "Spain"},{"Name": "Bon app'","City": "Marseille","Country": "France"},{"Name": "Bottom-Dollar Marketse","City": "Tsawassen","Country": "Canada"},{"Name": "Cactus Comidas para llevar","City": "Buenos Aires","Country": "Argentina"},{"Name": "Centro comercial Moctezuma","City": "Mxico D.F.","Country": "Mexico"},{"Name": "Chop-suey Chinese","City": "Bern","Country": "Switzerland"},{"Name": "Comrcio Mineiro","City": "So Paulo","Country": "Brazil"}] }?
AngularJS $http
AngularJS $http 是一個(gè)用于讀取web服務(wù)器上數(shù)據(jù)的服務(wù)。
$http.get(url) 是用于讀取服務(wù)器數(shù)據(jù)的函數(shù)。
<div ng-app="myApp" ng-controller="customersCtrl"> <ul><li ng-repeat="x in names">{{ x.Name ', ' x.Country }}</li> </ul></div> <script type="text/javascript" src="js/angular.min.js"></script> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) {$http.get("js/text.json").success(function(response) {$scope.names = response.records;}); }); </script>結(jié)果:
- Alfreds Futterkiste, Germany
- Ana Trujillo Emparedados y helados, Mexico
- Antonio Moreno Taquera, Mexico
- Around the Horn, UK
- B's Beverages, UK
- Berglunds snabbk枚p, Sweden
- Blauer See Delikatessen, Germany
- Blondel pre et fils, France
- Blido Comidas preparadas, Spain
- Bon app', France
- Bottom-Dollar Marketse, Canada
- Cactus Comidas para llevar, Argentina
- Centro comercial Moctezuma, Mexico
- Chop-suey Chinese, Switzerland
- Comrcio Mineiro, Brazil ??
應(yīng)用解析:
AngularJS 應(yīng)用通過(guò)?ng-app?定義。應(yīng)用在 <div> 中執(zhí)行。
ng-controller?指令設(shè)置了?controller 對(duì)象?名。
函數(shù)?customersController?是一個(gè)標(biāo)準(zhǔn)的 JavaScript?對(duì)象構(gòu)造器。
控制器對(duì)象有一個(gè)屬性:?$scope.names。
$http.get()?從web服務(wù)器上讀取靜態(tài)?JSON 數(shù)據(jù)。
服務(wù)器數(shù)據(jù)文件為: ?我這兒是寫(xiě)了text.json。
當(dāng)從服務(wù)端載入 JSON 數(shù)據(jù)時(shí),$scope.names?變?yōu)橐粋€(gè)數(shù)組。
?注:以上代碼也可以用于讀取數(shù)據(jù)庫(kù)數(shù)據(jù)。
轉(zhuǎn)載地址:http://www.runoob.com/angularjs/angularjs-http.html
更多專(zhuān)業(yè)前端知識(shí),請(qǐng)上 【猿2048】www.mk2048.com 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的AngularJS XMLHttpRequest的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 移动前端—H5实现图片先压缩再上传
- 下一篇: swfobject.js视频播放插件