AngularJS XMLHttpRequest

$http 是 AngularJS 中的一個核心服務,用於讀取遠程服務器的數據。javascript

讀取 JSON 文件

下是存儲在web服務器上的 JSON 文件:php

{
    "records": [
        {
            "Name": "Alfreds Futterkiste",
            "City": "Berlin",
            "Country": "Germany"
        },
        {
            "Name": "Ana Trujillo Emparedados y helados",
            "City": "Mxico D.F.",
            "Country": "Mexico"
        },
        {
            "Name": "Antonio Moreno Taquera",
            "City": "Mxico 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 pre et fils",
            "City": "Strasbourg",
            "Country": "France"
        },
        {
            "Name": "Blido 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": "Mxico D.F.",
            "Country": "Mexico"
        },
        {
            "Name": "Chop-suey Chinese",
            "City": "Bern",
            "Country": "Switzerland"
        },
        {
            "Name": "Comrcio Mineiro",
            "City": "So Paulo",
            "Country": "Brazil"
        }
    ]
}

 

AngularJS $http

AngularJS $http 是一個用於讀取web服務器上數據的服務。html

$http.get(url) 是用於讀取服務器數據的函數。java

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

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

</div>
<script type="text/javascript" src="js/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("js/text.json")
    .success(function(response) {$scope.names = response.records;});
});
</script>

結果:angularjs

  • Alfreds Futterkiste, Germany
  • Ana Trujillo Emparedados y helados, Mexico
  • Antonio Moreno Taquera, Mexico
  • Around the Horn, UK
  • B's Beverages, UK
  • Berglunds snabbk枚p, Sweden
  • Blauer See Delikatessen, Germany
  • Blondel pre et fils, France
  • Blido Comidas preparadas, Spain
  • Bon app', France
  • Bottom-Dollar Marketse, Canada
  • Cactus Comidas para llevar, Argentina
  • Centro comercial Moctezuma, Mexico
  • Chop-suey Chinese, Switzerland
  • Comrcio Mineiro, Brazil   

應用解析:web

AngularJS 應用經過 ng-app 定義。應用在 <div> 中執行。數據庫

ng-controller 指令設置了 controller 對象 名。json

函數 customersController 是一個標準的 JavaScript 對象構造器數組

控制器對象有一個屬性: $scope.names服務器

$http.get() 從web服務器上讀取靜態 JSON 數據

服務器數據文件爲:  我這兒是寫了text.json。

當從服務端載入 JSON 數據時,$scope.names 變爲一個數組。

 注:以上代碼也能夠用於讀取數據庫數據。

轉載地址:http://www.runoob.com/angularjs/angularjs-http.html

相關文章
相關標籤/搜索