AngularJS——5、Http

$http.get(url) 讀取服務器數據。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">  
        <script src="//apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>  
    </head>
    <body>
        <div ng-app="xxApp" ng-controller="xxCtrl"> 
            <ol>
                <li ng-repeat="x in names">
                    {{ x.Name + ', ' + x.City + ', ' + x.Country }}
                </li>
            </ol>
        </div>
        <script>
            var app = angular.module('xxApp', []);
            app.controller('xxCtrl', function($scope, $http) {
                $http.get("/xxJSON.php")
                     .success(function (response) {
                         //從服務端引入JSON數據,$scope.names變成一個數組
                         $scope.names = response.records;
                      });
            });
        </script>
    </body>
</html>

xxJSON.php

{"records":[
    {   
        "Name" : "YY",
        "City" : "Beijing",
        "Country" : "China"   
    },
    {
        "Name" : "FF",
        "City" : "Chengdu",
        "Country" : "China"
    },
    {
        "Name" : "LL",
        "City" : "Shanghai",
        "Country" : "China"
    }
]}

輸出:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章