What is the best way to use AngularJS to consume REST web services?

505 views
Skip to first unread message

Sam

unread,
Feb 1, 2014, 1:27:18 AM2/1/14
to ang...@googlegroups.com
There are a few options for consuming REST web services with AngularJS. A google search turned out $resource, $http and restangular. I am very new to Angularjs. Can someone more experienced which is the developer-friendly way to consume web services for a newbie?

Thank you very much.

Alec Taylor

unread,
Feb 1, 2014, 2:24:21 AM2/1/14
to ang...@googlegroups.com
Depends what you want to do. You'll likely want to use $http; and if
the REST API is your own, you might want to look into metawidget for
form generation.

Remember to modularise, e.g.: to services.js and controllers.js
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to angular+u...@googlegroups.com.
> To post to this group, send email to ang...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/groups/opt_out.

Joshua Wright

unread,
Feb 1, 2014, 4:32:00 PM2/1/14
to ang...@googlegroups.com
Read about $resource:  http://www.bennadel.com/blog/2433-Using-RESTful-Controllers-In-An-AngularJS-Resource.htm

It allows you to abstract your RESTful resources up a level from the more bare-metal $http

Michael Hopper

unread,
Feb 2, 2014, 1:38:09 AM2/2/14
to ang...@googlegroups.com
I just made the switch from $http to Restangular and really recommend it. Restangular has a ton of useful features.
I recommend putting the HTTP requests in service methods and have those methods return the promises to the controllers.
Here is an example:

// Controller code
$scope.getCustomers = function() {
  return customerService.getCustomers().then(function(customers) {
    $scope.model.customers = customers;
  }, function(response) {
    $log.error('response = ' + JSON.stringify(response));
  });
};

// Customer Service code
var customersUrl = Restangular.all('/customers/');
this.getCustomers = function() {
  return customersUrl.getList();
};

calvin crane

unread,
Feb 2, 2014, 6:37:18 AM2/2/14
to ang...@googlegroups.com
Here is a fully working example of using http with the google API. - https://github.com/landed1/landed1.github.io

Joe Gaudet

unread,
Feb 2, 2014, 1:33:37 PM2/2/14
to ang...@googlegroups.com
Restangular has a very nice wrapper around http  https://github.com/mgonto/restangular
Reply all
Reply to author
Forward
0 new messages