AngularJS XMLHttpRequest


$http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据。


读取 JSON 文件

以下是存储在web服务器上的 JSON 文件:

Customers_JSON.php

  1. [{
  2. "Name":"Alfreds Futterkiste",
  3. "City":"Berlin",
  4. "Country":"Germany"
  5. },{
  6. "Name":"Berglunds snabbköp",
  7. "City":"Luleå",
  8. "Country":"Sweden"
  9. },{
  10. "Name":"Centro comercial Moctezuma",
  11. "City":"México D.F.",
  12. "Country":"Mexico"
  13. },{
  14. "Name":"Ernst Handel",
  15. "City":"Graz",
  16. "Country":"Austria"
  17. },{
  18. "Name":"FISSA Fabrica Inter. Salchichas S.A.",
  19. "City":"Madrid",
  20. "Country":"Spain"
  21. },{
  22. "Name":"Galería del gastrónomo",
  23. "City":"Barcelona",
  24. "Country":"Spain"
  25. },{
  26. "Name":"Island Trading",
  27. "City":"Cowes",
  28. "Country":"UK"
  29. },{
  30. "Name":"Königlich Essen",
  31. "City":"Brandenburg",
  32. "Country":"Germany"
  33. },{
  34. "Name":"Laughing Bacchus Wine Cellars",
  35. "City":"Vancouver",
  36. "Country":"Canada"
  37. },{
  38. "Name":"Magazzini Alimentari Riuniti",
  39. "City":"Bergamo",
  40. "Country":"Italy"
  41. },{
  42. "Name":"North/South",
  43. "City":"London",
  44. "Country":"UK"
  45. },{
  46. "Name":"Paris spécialités",
  47. "City":"Paris",
  48. "Country":"France"
  49. },{
  50. "Name":"Rattlesnake Canyon Grocery",
  51. "City":"Albuquerque",
  52. "Country":"USA"
  53. },{
  54. "Name":"Simons bistro",
  55. "City":"København",
  56. "Country":"Denmark"
  57. },{
  58. "Name":"The Big Cheese",
  59. "City":"Portland",
  60. "Country":"USA"
  61. },{
  62. "Name":"Vaffeljernet",
  63. "City":"Århus",
  64. "Country":"Denmark"
  65. },{
  66. "Name":"Wolski Zajazd",
  67. "City":"Warszawa",
  68. "Country":"Poland"
  69. }]
 
 

AngularJS $http

  1. AngularJS $http 是一个用于读取web服务器上数据的服务。
  2. $http.get(url)是用于读取服务器数据的函数。
 
  1. <div ng-app="myApp" ng-controller="customersCtrl">
  2. <ul>
  3. <li ng-repeat="x in names">
  4. {{ x.Name+', '+ x.Country}}
  5. </li>
  6. </ul>
  7. </div>
  8. <script>
  9. var app = angular.module('myApp',[]);
  10. app.controller('customersCtrl',function($scope,$http){
  11. $http.get("Customers_JSON.php")
  12. .success(function(response){$scope.names = response.records;});
  13. });
  14. </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 文件内容为:
  1. {"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内容为:
 
  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="renderer" content="webkit">
  6. <!--360,以webkit内核进行渲染-->
  7. <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  8. <!--以最新内核进行渲染。-->
  9. <meta http-equiv="Cache-Control" content="no-siteapp"/>
  10. <!--百度禁止转码-->
  11. <title>moyu demo</title>
  12. <meta name="keywords" content="demo 测试 魔芋">
  13. <meta name="description" content="魔芋的测试示例">
  14. <meta name="robots" content="index,follow">
  15. <!--定义网页搜索引擎索引方式-->
  16. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  17. <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
  18. <script src="http://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js"></script>
  19. <style>
  20. </style>
  21. </head>
  22. <body>
  23. <div ng-app="myApp" ng-controller="customersCtrl">
  24. <ul>
  25. <li ng-repeat="x in names">
  26. {{ x.Name+', '+ x.Country}}
  27. </li>
  28. </ul>
  29. </div>
  30. <script>
  31. var app = angular.module('myApp',[]);
  32. app.controller('customersCtrl',function($scope, $http){
  33. $http.get("test.php")
  34. .success(function(response){
  35. $scope.names = response.records;
  36. });
  37. });
  38. </script>
  39. </body>
  40. </html>
 
结果:
 
 
 
 
 
 
 
 
 
 
 
**

【08】AngularJS XMLHttpRequest的更多相关文章

  1. SSAS系列——【08】多维数据(程序展现Cube)

    原文:SSAS系列--[08]多维数据(程序展现Cube) 1.引用DLL? 按照之前安装的MS SQLServer的步骤安装完成后,发现在新建的项目中“Add Reference”时居然找不到Mic ...

  2. 【08】css sprite是什么,有什么优缺点

    [08]css sprite是什么?有什么优缺点? 概念:将多个小图片拼接到一个图片中.通过background-position和元素尺寸调节需要显示的背景图案. 优点: 减少HTTP请求数,极大地 ...

  3. 【转】angularjs指令中的compile与link函数详解

    这篇文章主要介绍了angularjs指令中的compile与link函数详解,本文同时诉大家complie,pre-link,post-link的用法与区别等内容,需要的朋友可以参考下   通常大家在 ...

  4. 【转】AngularJS的$resource

    $http $http服务是基于$q服务的,提供了promise封装,它接受一个配置对象参数,并返回一个promise对象.同时,它还提供了2个方法用来定义Promise回调:success 和 er ...

  5. 【转】AngularJS 取消对 HTML 片段的转义

    今天尝试用 Rails 做后端提供 JSON 格式的数据, AngularJS 做前端处理 JSON 数据,其中碰到 AngularJS 获取的是一段 HTML 文本,如果直接使用 data-ng-b ...

  6. 【经验】Angularjs 中使用 layDate 日期控件

    layDate 控件地址:http://laydate.layui.com/ 前情:原来系统中使用的日期控件是UI bootstrap(地址:https://angular-ui.github.io/ ...

  7. 【转】AngularJS路由和模板

    1. AngularJS路由介绍 AngularJS路由功能是一个纯前端的解决方案,与我们熟悉的后台路由不太一样.后台路由,通过不同的URL会路由到不同的控制器上(controller),再渲染(re ...

  8. 【经验】angularjs 实现带查找筛选功能的select下拉框

    一.背景 对于select的下拉列表,像国家选择这样的功能,全世界那么多国家,一直拉滚动条多辛苦,眼睛也要盯着找,累!so,为优化用户体验,带查找功能的下拉框是非常非常有必要的.都知道jquery里有 ...

  9. 【转】Angularjs Controller 间通信机制

    在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...

随机推荐

  1. Postgresql的一些命令

    显示所有数据表: \dt 显示表结构:  \d YOUR_TABLE 进入数据库: psql DATABASE_NAME 显示所有数据库: \list 退出: \q 删除数据库: dropdb DAT ...

  2. [Swift]Set(集)转换为Array(数组)

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. zabbix详细介绍及其自动动态发现

    zabbix3.2.1 第1章 安装 1.1 查看系统环境 [root@centos7-2 ~]# [root@centos7-2 ~]# hostname -I 10.0.0.10 172.16.1 ...

  4. IBatis.NET 的配置

    http://www.cnblogs.com/xiaogangqq123/archive/2011/06/29/2093250.html http://www.cnblogs.com/hjf1223/ ...

  5. ACM_01背包

    背包1 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量不超过 W ...

  6. CodeDOMProvider 类

    CodeDomProvider 可用来创建和检索代码生成器和代码编译器的实例.代码生成器可以生成特定语言的代码,如:C#.Visual Basic.JScript 等,而代码编译器可以将代码文件编译成 ...

  7. CF869C The Intriguing Obsession

    思路: 分别在两种不同颜色的岛屿群之间进行搭桥.因为相同颜色的岛屿之间不能有边,任意两个相同颜色的岛屿不能同时和另外一个不同颜色的岛屿都有边.实现: #include <bits/stdc++. ...

  8. FTP初始化文件.netrc使用技巧[转发]

    FTP初始化文件.netrc使用技巧 FTP(文件传输)和E-mail(电子邮件).Telnet(远程登录)一样,是 Internet的三大主要功能之一.因为使用频繁,用户往往会遇到各种 各样的问题, ...

  9. 【C++】智能指针简述(二):auto_ptr

    首先,我要声明auto_ptr是一个坑!auto_ptr是一个坑!auto_ptr是一个坑!重要的事情说三遍!!! 通过上文,我们知道智能指针通过对象管理指针,在构造对象时完成资源的分配及初始化,在析 ...

  10. ASP MVC

    V-view 显示层 C-controller 控制层 M-model 模型 D-database 数据库 S-Service 服务 D-Database/Dao 数据库/访问数据库的方法 View即 ...