AngularJS XMLHttpRequest
生活随笔
收集整理的這篇文章主要介紹了
AngularJS XMLHttpRequest
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
$http?是 AngularJS 中的一個核心服務,用于讀取遠程服務器的數據。
讀取 JSON 文件
下是存儲在web服務器上的 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 是一個用于讀取web服務器上數據的服務。
$http.get(url) 是用于讀取服務器數據的函數。
<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>結果:
- 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 ??
應用解析:
AngularJS 應用通過?ng-app?定義。應用在 <div> 中執行。
ng-controller?指令設置了?controller 對象?名。
函數?customersController?是一個標準的 JavaScript?對象構造器。
控制器對象有一個屬性:?$scope.names。
$http.get()?從web服務器上讀取靜態?JSON 數據。
服務器數據文件為: ?我這兒是寫了text.json。
當從服務端載入 JSON 數據時,$scope.names?變為一個數組。
?注:以上代碼也可以用于讀取數據庫數據。
轉載地址:http://www.runoob.com/angularjs/angularjs-http.html
更多專業前端知識,請上 【猿2048】www.mk2048.com 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的AngularJS XMLHttpRequest的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 移动前端—H5实现图片先压缩再上传
- 下一篇: XML文档的简易增删查改