10009---AngularJS Http
来源:互联网 发布:淘宝做代理赚钱吗 编辑:程序博客网 时间:2024/05/21 02:48
http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据。
读取 JSON 文件
以下是存储在web服务器上的 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"}]
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("http://www.runoob.com/try/angularjs/data/Customers_JSON.php") .success(function(response) {$scope.names = response.records;});});</script>
应用解析:
注意:以上代码的 get 请求是本站的服务器,你不能直接拷贝到你本地运行,会存在跨域问题,解决办法就是将 Customers_JSON.php 的数据拷贝到你自己的服务器上,附:PHP Ajax 跨域问题最佳解决方案。
AngularJS 应用通过 ng-app 定义。应用在 <div> 中执行。
ng-controller 指令设置了 controller 对象 名。
函数 customersController 是一个标准的 JavaScript对象构造器。
控制器对象有一个属性: $scope.names。
$http.get() 从web服务器上读取静态 JSON 数据。
服务器数据文件为: http://www.runoob.com/try/angularjs/data/Customers_JSON.php。
当从服务端载入 JSON 数据时,$scope.names 变为一个数组。
*以上代码也可以用于读取数据库数据。
0 0
- 10009---AngularJS Http
- angularjs $http
- AngularJS $http
- angularjs $http
- AngularJS http
- AngularJS $http
- AngularJs(四):$Http,$resource
- AngularJS $http Service
- AngularJS $http使用
- AngularJS中$http
- AngularJS(03)---Http对象
- AngularJS $http service
- AngularJS $http简介1
- AngularJS - $http的config
- AngularJS for $http
- AngularJS中http拦截
- AngularJS中的$http
- $http AngularJS异步请求
- 测试博客
- 【Java】Java、JavaScript、JSP、JScript,傻傻分不清楚
- HDU 5146 Sequence
- 【Ruby】语法记录一
- mysql下载安装配置
- 10009---AngularJS Http
- 阿里的分布式数据库DRDS
- Map中entry的源代码
- Android学习笔记(3):Android项目结构分析
- 【ZYNQ-7000开发之三】ZYNQ平台的HDMI驱动测试
- 第14章1节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-面向控件编程VS面向坐标编程
- 第14章2节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-HierarchyViewer架构概述
- Evaluate Reverse Polish Notation
- Android学习笔记(4):资源字典R.java