angularJS $http 服务

AngularJS $http

AngularJS $http 是一个用于读取web服务器上数据的服务。php

$http.get(url) 是用于读取服务器数据的函数。html

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

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

 

应用解析:web

注意:以上代码的 get 请求是本站的服务器,你不能直接拷贝到你本地运行,会存在跨域问题,解决办法就是将 Customers_JSON.php 的数据拷贝到你本身的服务器上,附:PHP Ajax 跨域问题最佳解决方案ajax

AngularJS 应用经过 ng-app 定义。应用在 <div> 中执行。跨域

ng-controller 指令设置了 controller 对象 名。数组

函数 customersController 是一个标准的 JavaScript 对象构造器服务器

控制器对象有一个属性: $scope.namesapp

 

$http.get() 从web服务器上读取静态 JSON 数据函数

服务器数据文件为:  http://www.runoob.com/try/angularjs/data/Customers_JSON.php

当从服务端载入 JSON 数据时,$scope.names 变为一个数组。

  <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>
相关文章
相关标签/搜索