Open In App

What is the difference between of using Restangular over ngResource ?

Last Updated : 17 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Key differences between Restangular and ngResource:

  • Instead of doing the “magic” filling of objects like $resource, Restangular uses promises(promise represents the eventual results of an operation. You’ll use a promise to specify what to try when an operation succeeds or fails).
  • Restangular doesn’t have a problem with trailing slashes, additional: within the URL, escaping information, expecting only arrays for getting lists, etc.
  • Restangular supports all HTTP methods whereas, $resource supports only ‘GET’, ‘POST’, ‘PUT’, ‘DELETE’.
  • Restangular supports Etag of the box. You do not need to do anything. ETags and If-None-Match are going to be utilized in all of your requests.
  • In Restangular, if you receive from the server some item that features a link to itself, you’ll use that to question the server rather than writing the URL manually.
  • Each time you would like to try to an invitation, you’ll just roll in the hay using the thing that was returned by Restangular. you do not get to create a replacement object for this. But, in while using $resource you’ve got to make one $resource object per request.
  • With $resource, you would like to write down the URL Template. In Restangular, you do not write any URLs. you only write the name of the resource you would like to fetch and that is it.
  • If you’ve got Nested RESTful resources, Restangular can handle them for you. you do not need to know the URL, the path, or anything to try to all of the HTTP operations you would like.

Javascript




// Restangular returns promises
Restangular.all('users').getList() // GET: /users
    .then(function (users) {
  
        // Returns an inventory of users
        // First Restangular obj in list: { id: 123 }
        $scope.user = users[0]; 
    })
// code
  
// Restangular objects are self-aware and 
// skills to form their own RESTful requests
// GET: /users/123/cars
$scope.user.getList('cars'); 
  
// You'll also use your own custom methods
// on Restangular objects
// POST: /users/123/sendMessage
$scope.user.sendMessage(); 
  
// Chain methods together to simply
// build complex requests
$scope.user.one('messages', 123)
    .one('from', 123).getList('unread');
// GET: /users/123/messages/123/from/123/unread


In a nutshell, we will say that besides the extra features and therefore the promise based approach, Restangular also can handle all of your URLs, in order that you don’t need to know anything about them.

Javascript




Restangular.one("users", 123).get().then(function(user) {
    $scope.user = user;
});
  
// code
  
// Automatically does the request to /users/123/cars 
// because it remembers during which object you're asking it.
$scope.user.getList('cars')




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads