Angular $http
| Objective |
|---|
| Use Angular $http to query an API and CRUD one resource |
The $http service is a core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP.
Set Up
$http will be a dependency in your Angular controller, so before including it, make sure you have an Angular app and controller set up.
Define your Angular app in
app.jsand include it in the<html>tag inindex.html.// app.js angular.module('exampleApp', []);<!-- index.html --> <html ng-app="exampleApp"> <head> <title>Example Angular App</title> <!-- angular --> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular.min.js"></script> <!-- custom script --> <script type="text/javascript" src="app.js"></script> </head> <body> ... </body> </html>Define an Angular controller and include the $http dependency. Don't forget to also include the controller in the
<body>tag inindex.html.// app.js angular.module('exampleApp', []) .controller('MainCtrl', ['$scope', '$http', function ($scope, $http) { // controller logic }]);<!-- index.html --> <html ng-app="exampleApp"> <head> <title>Example Angular App</title> <!-- angular --> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.4/angular.min.js"></script> <!-- custom script --> <script type="text/javascript" src="app.js"></script> </head> <body ng-controller="MainCtrl"> ... </body> </html>
$http Syntax
1. Long-Form
Similar to jQuery's $.ajax()
$http({method: 'GET', url: '/someUrl'})
.then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status
});
2. Shortcut Methods
Similar to jQuery's $.get() and $.post().
// GET request
$http.get('/someUrl')
.then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
// POST request (passing data)
$http.post('/someUrl', {msg:'hello word!'})
.then(function(response) {
// this callback will be called asynchronously
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Read more about the available shortcut methods in the $http docs.
Challenges
We're going to be using Angular $http and the DareToDiscover API to build a wine discovery app. DareToDiscover is a RESTful API, so its request syntax follows this pattern:
Base URL: http://daretodiscover.herokuapp.com
| HTTP Verb | URL | Functionality |
|---|---|---|
| GET | /wines | READS all wines |
| POST | /wines | CREATES new wine |
| GET | /wines/:id | READS one wine |
| PUT | /wines/:id | UPDATES one wine |
| DELETE | /wines/:id | DESTROYS one wine |
Open up Postman and make a request to get all wines. Make sure the response is a list of wine objects, and familiarize yourself with how the data is structured.
Set up a new Angular project with an
app.jsand anindex.html.- Your
app.jsshould have:- Angular app defined (
angular.module( ... ))
- Angular app defined (
- Your
index.htmlshould have:- Angular CDN
app.jsscriptng-appin either the<html>tag
- Your
Make a new controller called
winesCtrland include$httpas a dependency. Hint: Make sure you addwinesCtrlto your Angular app inapp.js, and useng-controllerto include it in the<body>tag inindex.html.When a user opens your app, they should see a list of all the wines from DareToDiscover. Display all the wine attributes, including the photo. Hint: Look up
ng-srcfor images.Make a form to create a new wine. When a user submits the form, it should send an
$httprequest to CREATE a new wine in the DareToDiscover database.Each wine in your list should have an edit button. When a user clicks the edit button, the wine information should hide and the edit form should show.
When a user submits the edit form, send an
$httprequest to UPDATE the wine in the DareToDiscover database.Implement a delete button. When the user clicks it, send an
$httprequest to DESTROY the wine from the DareToDiscover database.
Stretch Challenge
Link the name of each wine to a view that shows only the details for that wine. Hints:
- Use
ngRouteandng-viewto set up multiple views in your Angular app. - Use
$routeParamsto figure out which wine to display. - Your view for a single wine will have a different controller than your view that displays all wines.