码迷,mamicode.com
首页 > Web开发 > 详细

AngularJS Http

时间:2016-07-11 18:34:29      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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

读取 JSON 文件

{
    "records": [
        {
            "Name": "Alfreds Futterkiste",
            "City": "Berlin",
            "Country": "Germany"
        }, {
            "Name": "Ana Trujillo Emparedados y helados",
            "City": "México D.F.",
            "Country": "Mexico"
        }, {
            "Name": "Antonio Moreno Taquería",
            "City": "México D.F.",
            "Country": "Mexico"
        }, {
            "Name": "Around the Horn",
            "City": "London",
            "Country": "UK"
        }, {
            "Name": "B‘s Beverages",
            "City": "London",
            "Country": "UK"
        }, {
            "Name": "Berglunds snabbköp",
            "City": "Luleå",
            "Country": "Sweden"
        }, {
            "Name": "Blauer See Delikatessen",
            "City": "Mannheim",
            "Country": "Germany"
        }, {
            "Name": "Blondel père et fils",
            "City": "Strasbourg",
            "Country": "France"
        }, {
            "Name": "Bólido Comidas preparadas",
            "City": "Madrid",
            "Country": "Spain"
        }, {
            "Name": "Bon app‘",
            "City": "Marseille",
            "Country": "France"
        }, {
            "Name": "Bottom-Dollar Marketse",
            "City": "Tsawassen",
            "Country": "Canada"
        }, {
            "Name": "Cactus Comidas para llevar",
            "City": "Buenos Aires",
            "Country": "Argentina"
        }, {
            "Name": "Centro comercial Moctezuma",
            "City": "México D.F.",
            "Country": "Mexico"
        }, {
            "Name": "Chop-suey Chinese",
            "City": "Bern",
            "Country": "Switzerland"
        }, {
            "Name": "Comércio Mineiro",
            "City": "São Paulo",
            "Country": "Brazil"
        }
    ]
}

AngularJS $http

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

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

<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>

<body>

    <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("Customers.json")
                .success(function(response) {
                    $scope.names = response.records;
                });
        });
    </script>

</body>

</html>

应用解析:

注意:以上代码的 get 请求是本地Customers.json文件。

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

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

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

$http.get() 读取静态 JSON 数据$scope.names 变为一个数组。

 

AngularJS Http

标签:

原文地址:http://www.cnblogs.com/chrisghb8812/p/5661109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!