AngularJS Http

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

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

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

    <ul>

        <li ng-repeat="x in names"> {{ x.Name + ', ' + x.Country }} </li>

    </ul>

</div>

<script>

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

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

        $http.get("http://www.runoob.com/try/angularjs/data/sites.php")

        .success(function (response) {$scope.names = response.sites;}); });

</script>

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

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

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

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

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

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

原文地址:https://www.cnblogs.com/miny-simp/p/6000592.html