AngularJS (05) $http

$http es un servicio del que dispone AngularJS para poder leer datos desde servidores remotos.

Por ejemplo, consideremos el siguiente archivo con un objeto JSON, que esta ubicado en:

http://www.w3schools.com/angular/customers.php
[
{
"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"
}
]

Utilizando la funcion: $http.get(url) se pueden leer los datos como en este ejemplo:

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<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("http://www.w3schools.com/angular/customers.php")
  .success(function (response) {$scope.names = response.records;});
});
</script>

</body>
</html>
  • La aplicación comienza inicializándose con ng-app
  • La directiva ng-controller llama al controlador del objeto
  • La función costumercontroler es un constructor de javascript
  • $http.get(url) lee los datos JSON desde un servidor
  • $scope.names hace un array cuando se leen los datos JSON desde el archivo en el servidor