[英]Restangular - Use custom methods with service
I'm using a Decoupled Restangular service for consuming my API: 我正在使用Decoupled Restangular服务来使用我的API:
// Declare factory
module.factory('Users', function(Restangular) {
return Restangular.service('users');
});
I'd like to call a custom method on my resource: /users/active
The documentation says I can do it like so: 我想在我的资源上调用自定义方法:
/users/active
文档说我可以这样做:
// GET /users/active
Restangular.all("users").customGET("active")
However, I'd like to use the service I created before, rather than redeclaring a restangular resource 'users', which wouldn't be completely "DRY". 但是,我想使用之前创建的服务,而不是重新声明不是“ DRY”的矩形资源“用户”。
I'd like to do something like: 我想做类似的事情:
Users.customGET("active")
But I get an error TypeError: Users.customGET is not a function. 但是我收到一个错误TypeError:Users.customGET不是一个函数。
您可以使用with config进行尝试,例如
return Restangular.withConfig(function(config){ config.addElementTransformer('realm',true,function(worker){ worker.addRestangularMethod('getActive','get'); return worker; }); }).service('users');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.