【08】AngularJS XMLHttpRequest
AngularJS XMLHttpRequest
$http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据。
读取 JSON 文件
以下是存储在web服务器上的 JSON 文件:
Customers_JSON.php
[{"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
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>var app = angular.module('myApp',[]);app.controller('customersCtrl',function($scope,$http){$http.get("Customers_JSON.php").success(function(response){$scope.names = response.records;});});</script>
应用解析:
注意:以上代码的 get 请求是本站的服务器,你不能直接拷贝到你本地运行,会存在跨域问题,解决办法就是将 Customers_JSON.php 的数据拷贝到你自己的服务器上。
AngularJS 应用通过 ng-app 定义。应用在 <div> 中执行。
ng-controller 指令设置了 controller 对象 名。
函数 customersController 是一个标准的 JavaScript 对象构造器。
控制器对象有一个属性: $scope.names。
$http.get() 从web服务器上读取静态 JSON 数据。
当从服务端载入 JSON 数据时,$scope.names 变为一个数组。
魔芋练习:
其中,test.php 文件内容为:
{"records":[{"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"},{"Name":"Ana Trujillo Emparedados y helados","City":"México D.F.","Country":"Mexico"},{"Name":"Antonio Moreno Taquería","City":"México 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 père et fils","City":"Strasbourg","Country":"France"},{"Name":"Bólido 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":"México D.F.","Country":"Mexico"},{"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"},{"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"}]}
demo.html内容为:
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8"><meta name="renderer" content="webkit"><!--360,以webkit内核进行渲染--><meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"><!--以最新内核进行渲染。--><meta http-equiv="Cache-Control" content="no-siteapp"/><!--百度禁止转码--><title>moyu demo</title><meta name="keywords" content="demo 测试 魔芋"><meta name="description" content="魔芋的测试示例"><meta name="robots" content="index,follow"><!--定义网页搜索引擎索引方式--><meta name="viewport" content="width=device-width, initial-scale=1.0"><script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script><script src="http://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js"></script><style></style></head><body><div ng-app="myApp" ng-controller="customersCtrl"><ul><li ng-repeat="x in names">{{ x.Name+', '+ x.Country}}</li></ul></div><script>var app = angular.module('myApp',[]);app.controller('customersCtrl',function($scope, $http){$http.get("test.php").success(function(response){$scope.names = response.records;});});</script></body></html>
结果:

**




浙公网安备 33010602011771号