我的服务
app.factory('PartnerServiceRestangular', ['Restangular', function(Restangular) {
return Restangular.service('partner');
}]);
app.factory('PartnerInvoiceServiceRestangular', ['PartnerServiceRestangular', function(PartnerServiceRestangular) {
return PartnerServiceRestangular.one('partner', partnerId).service('invoice');
}]);
我的 Controller
app.controller('FinancialsController', [
'$scope',
'PartnerServiceRestangular',
'PartnerInvoiceServiceRestangular',
function($scope, PartnerServiceRestangular, PartnerInvoiceServiceRestangular) {
PartnerServiceRestangular.getList().then(function(partners) {
$scope.partners = partners;
});
$scope.partnerChange = function(partner) {
$scope.partner = partner;
};
var getInvoices = function(partnerId, startDate, stopDate) {
// STUCK HERE
PartnerInvoiceServiceRestangular.get({})
};
我想要做的是允许
PartnerInvoiceServiceRestangular
执行以下获取请求:/partner/{partnerId}/invoice/
- 返回特定合作伙伴 ID 的所有发票 /partner/{partnerId}/invoice/?startDate=2014-06-01&stopDate=2014-6-30
- 返回 2 个给定日期之间的所有发票 /partner/{partnerId}/invoice/{invoiceId}
- 返回特定发票 我已经在 View 中设置了 ng-models 设置
partnerId
、 startDate
、 stopDate
的 $scopes 最佳答案
像这样的事情应该有效。
app.factory('PartnerInvoiceServiceRestangular', ['PartnerServiceRestangular', function(PartnerServiceRestangular) {
return {
getService: function(partnerId) {
PartnerServiceRestangular.one('partner', partnerId).service('invoice');
}
}
}]);
var getInvoices = function(partnerId, startDate, stopDate) {
PartnerInvoiceServiceRestangular.getService(partnerId)
.getList({ startDate: startDate, stopDate: stopDate }).then(function(invoices) {
$scope.partner.invoices = invoices;
}
};
但是,我不喜欢您的工厂服务实现。为什么不使用:
$scope.partnerChange = function(partner) {
$scope.partner = partner;
partner.getList('invoice', { startDate: startDate, stopDate: stopDate }).then(function(invoices) {
$scope.partner.invoices = invoices;
});
};
关于javascript - 工厂内的 Restangular 嵌套 Rest 资源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27245906/