Using promises with Restangular

Restangular, the extremely popular REST API extension to AngularJS, takes a much more promise-centric approach compared to $resource.

How to do it…

The Restangular REST API mapping will always return a promise. This is shown here:

(app.js) angular.module('myApp', ['restangular']) .controller('Ctrl', function($scope, Restangular) { Restangular .one('widget', 4) // get() will return a promise for the GET request .get() .then( function(data) { // consume response data in success handler $scope.status = 'One widget success!'; }, function(response) { // consume response message in error handler $scope.status = 'One widget failure!'; } ); // generally, the API mapping is stored in a variable, // and the promise-returning ...

Get AngularJS Web Application Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.