Escolar Documentos
Profissional Documentos
Cultura Documentos
get Example
On this page we will provide AngularJS $http.get example. $http.get is a shortcut for HTTP GET
method. $http is a core angular service to communicate with remote HTTP services. $http.get
accepts a HTTP URL and an optional configuration object. On the promise object returned by
$http.get, we can call .then callback or .success callback. .success has been deprecated. Here on
this page we will provide example for both .then and .success.
$http.get
AngularJS $http.get() is a shortcut to perform HTML form GET method. The syntax is given
below.
get(url, [config]);
Where url is a HTTP URL that needs to be accessed and config is a configuration object and it is
optional.$http.get returns HTTP promise object on which we can call .then along
with .catch and .finally. On promise object we can also call .success along
with .error and .finally.
promise in AngularJS
AngularJS promise is a HTTP promise that will run asynchronously. It is a concept in which a
HTTP request started to execute asynchronously such as fetching a URL that can either be
successful or fail. promise object is provided by AngularJS $q service. On the promise object
we can call other AngularJS functions. $http.get returns promiseobject.
In .then() we get response object and using this object we get the properties as response.data,
response.status etc. Now find sample JSON data used in our example.
sample_json.js
[
{
"id" : "1",
"name" : "Shankar",
"location" : "Varanasi"
},
{
"id" : "2",
"name" : "Ram",
"location" : "Ayodhya"
},
{
"id" : "3",
"name" : "Krishna",
"location" : "Mathura"
}
]
http-get.html
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<ul>
<li ng-repeat="row in jsondata">
{{ row.id + ', ' + row.name + ', ' + row.location}} <br>
</li>
</ul>
</div>
</body>
</html>
Find the output.