admin管理员组

文章数量:1332382

I am trying to get some data from a REST api using mgonto's Restangular.

// Restangular returns promises
Restangular.all('users').getList()  // GET: /users
.then(function(users) {
  // returns a list of users
  $scope.user = users[0]; // first Restangular obj in list: { id: 123 }
})

// Later in the code...

// Restangular objects are self-aware and know how to make their own RESTful requests
$scope.user.getList('cars');  // GET: /users/123/cars

That's fine, and this works, but all the results returned in the promises callbacks have some methods and properties added by Restangular (this is how you can do .getList("cars") on a user).

What I want is to retrieve only the user's data (name, id...) without all the Restangular methods. Just a plain JS object.

I couldn't find any way to do this in the docs. Everytime I use a method on a returned user it always returns a wrapped object with the Restangular methods.

I am trying to get some data from a REST api using mgonto's Restangular.

// Restangular returns promises
Restangular.all('users').getList()  // GET: /users
.then(function(users) {
  // returns a list of users
  $scope.user = users[0]; // first Restangular obj in list: { id: 123 }
})

// Later in the code...

// Restangular objects are self-aware and know how to make their own RESTful requests
$scope.user.getList('cars');  // GET: /users/123/cars

That's fine, and this works, but all the results returned in the promises callbacks have some methods and properties added by Restangular (this is how you can do .getList("cars") on a user).

What I want is to retrieve only the user's data (name, id...) without all the Restangular methods. Just a plain JS object.

I couldn't find any way to do this in the docs. Everytime I use a method on a returned user it always returns a wrapped object with the Restangular methods.

Share asked Aug 27, 2015 at 15:49 RayjaxRayjax 7,78413 gold badges57 silver badges85 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

I guess you are looking for 'plain()' (alias for Restangular.stripRestangular(elem)).

plain(): Returns the plain element received from the server without any of the enhanced methods from Restangular. It's an alias to calling Restangular.stripRestangular(elem)

It strips all the restangular methods and returns the plain object which is returned by the server.

For more information please refer to the following link :

https://github./mgonto/restangular#element-methods

The following fiddle might help :

Fiddle ::http://plnkr.co/edit/oMFnYM4HkaFK3biscpTo?p=preview

本文标签: javascriptRestangularHow to get the request39s plain results (not wrapped)Stack Overflow