What is the difference between .all() and .one() in Restangular? What is the difference between .all() and .one() in Restangular? angularjs angularjs

What is the difference between .all() and .one() in Restangular?


The one() function has a second argument that accepts an id e.g. .one('users', 1).

  • one('users', 1).get() translates to /users/1
  • all('users').getList() translates to /users

Unlike all(), one() is not generally used with .getList() without argument. However, if you were to call .one('users', 1).getList('emails') or .one('users', 1).all('emails').getList(), then you would make a GET request to /users/1/emails.


My guess is that they are there for expressing an intention of what you are going to do. I would understand those as a way to build the url, expressing if you are accessing to the whole resource or to a specific one.

In the end, they are gonna build and do a GET request but because you do a GET and retrieve some data it does not mean that it should be used in that way.

Example extracted from https://github.com/mgonto/restangular/issues/450

getList can be called both ways. If it's called in an element one, then it needs a subelement to get to a Collection. Otherwise, it fetches the collection. So the following is the same:

Restangular.one('places', 123).getList('venues')        // GET /places/123/venuesRestangular.one('places', 123).all('venues').getList()  // GET /places/123/venues

As you can see, it is more expressive to call one('places', 123).all('venues') to understand that you just want the venues located in the area/place 123.

Maybe the following url will help you:

https://github.com/mgonto/restangular/issues/450


I've recently discovered a difference between these methods. Yes, both of them make the same get requests, but the results you get might surprise you (as they surprised me).

Let's assume we have an API method /users which returns not strictly an array, but something like this:

{   "result": [{...}]}

So an array is returned as a value of some prop of the response object. In this case get() and getList() work differently. This code works well:

Restangular.get('users').then(function (response) {...});

Your response handler gets invoked after response has been received. But this code doesn't seem to work:

Restangular.all('users').getList().then(function (response) {...});

Response handler is not invoked, despite that request completed with status code 200 and non-empty response. Browser console doesn't show any errors and network monitor shows successful request.

I've tested this with Restangular 1.5.2 so probably this is already fixed in newer versions.