javascript - Restangular - How to get the request's plain results (not wrapped) - Stack Overflow

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742294013a4416747.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信