AngularJS Tutorial(7)from w3school



$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

< 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("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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值