Using promises with $resource
As part of the ngResource
module, $resource
provides a service to manage connections with RESTful resources. As far as vanilla AngularJS goes, this is in some ways the closest you'll get to a formal data object model infrastructure. The $resource
tool is highly extensible and is an excellent standalone tool upon which to build applications if third-party libraries like Restangular aren't your cup of tea.
As the API-focused wrapper for $http
, $resource
also provides an interface for using promises in conjunction with the HTTP requests that it generates.
How to do it…
Although it wraps $http
, $resource
actually does not use promises in its default implementation. The $promise
property can be used to access the promise object of the HTTP request, as follows:
// creates the resource object, which exposes get(), post(), etc. var Widget = $resource('/widgets/:widgetId', {widgetId: '@id'}); // resource object must be coaxed into returning its promise // this can be done...