AngularJs Http

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

读取Json文件:http:192.168.1.1/myweb/index.aspx

[
{name:'tom',age:23,position:'IT tester'},
{name:'tony,age:22,position:'Manager''},
{name:'mary,age:19,position:'CEO''}
]


$http.get(url)读取服务器数据的函数:

<div ng-app="myApp" ng-controller="customersCtrl">

 <ul>

   <li ng-repeat="x in names">

     {{x.name+','+x.age+','+x.position}}

  </li>

 </ul>

</div>

<script>

   var app=angular.module('myApp',[]);

   app.controller('customersCtrl',function($scope,$http){

   $http.get("http:192.168.1.1/myweb/index.aspx").success(
function(response) { $scope.names=response.records; }); }); </script>

解析:通过ng-app定义应用在<div>中执行
ng-controller指令设置对象名。

customerscontroller是标准的js对象构造器,控制器对象有一个属性:

$scope.names,$http.get()从web服务器上读取静态json数据。

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

以上可演化为读取数据库中的数据到前台显示。

摘录自runoob.com。旨在通过自己的语言加深对AngularJS的学习、理解和记忆。

原文地址:https://www.cnblogs.com/professional-NET/p/4999381.html