AngularJS Tutorial(7)from w3school

来源:互联网 发布:网络流行语2016英文 编辑:程序博客网 时间:2024/06/01 08:21


$http is an AngularJS service for reading data from remote servers.


Providing Data

The following data can be provided by a web server:

http://www.w3schools.com/angular/customers.php

{
"records": [
  {
    "Name" : "Alfreds Futterkiste",
    "City" : "Berlin",
   "Country" : "Germany"
  },
  {
    "Name" : "Berglunds snabbköp",
    "City" : "Luleå",
    "Country" : "Sweden"
  },
  {
    "Name" : "Centro comercial Moctezuma",
    "City" : "México D.F.",
    "Country" : "Mexico"
  },
  {
   "Name" : "Ernst Handel",
    "City" : "Graz",
    "Country" : "Austria"
  },
 {
    "Name" : "FISSA Fabrica Inter. Salchichas S.A.",
    "City" : "Madrid",
   "Country" : "Spain"
  },
  {
    "Name" : "Galería del gastrónomo",
   "City" : "Barcelona",
    "Country" : "Spain"
  },
  {
    "Name" : "Island Trading",
    "City" : "Cowes",
    "Country" : "UK"
  },
  {
    "Name" : "Königlich Essen",
    "City" : "Brandenburg",
    "Country" : "Germany"
  },
  {
   "Name" : "Laughing Bacchus Wine Cellars",
    "City" : "Vancouver",
   "Country" : "Canada"
  },
  {
    "Name" : "Magazzini Alimentari Riuniti",
   "City" : "Bergamo",
    "Country" : "Italy"
  },
  {
    "Name" : "North/South",
    "City" : "London",
    "Country" : "UK"
  },
  {
   "Name" : "Paris spécialités",
    "City" : "Paris",
    "Country" : "France"
 },
  {
    "Name" : "Rattlesnake Canyon Grocery",
    "City" : "Albuquerque",
   "Country" : "USA"
  },
  {
    "Name" : "Simons bistro",
    "City" : "København",
   "Country" : "Denmark"
  },
  {
    "Name" : "The Big Cheese",
    "City" : "Portland",
    "Country" : "USA"
  },
  {
    "Name" : "Vaffeljernet",
   "City" : "Århus",
    "Country" : "Denmark"
  },
  {
    "Name" : "Wolski Zajazd",
    "City" : "Warszawa",
    "Country" : "Poland"
  }
]
}

AngularJS $http

AngularJS $http is a core service for reading data from web servers.

$http.get(url) is the function to use for reading server data.

AngularJS Example

<divng-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("http://www.w3schools.com/angular/customers.php")
    .success(function(response) {$scope.names = response.records;});
});
</script>

Try it Yourself »

Application explained:

The AngularJS application is defined by ng-app. The application runs inside a <div>.

The ng-controller directive names the controller object.

The customersCtrl function is a standard JavaScript object constructor.

AngularJS will invoke customersCtrl with a $scope and$http object.

$scope is the application object (the owner of application variables and functions).

 $http is an XMLHttpRequest object for requesting external data.

$http.get() reads JSON data from http://www.w3schools.com/angular/customers.php.

If success, the controller creates a property (names) in the scope, with JSON data from the server.

0 0
原创粉丝点击