我是angularjs和ui-router的新手,无法使用我传递给它的参数,以我想要的方式获取资源服务来格式化GET URL。
这是我的控制器代码:
views: {
'detail': {
templateUrl: 'app/receipts/receipts.detail.html',
resolve: {
receipt:
function( receiptAPI, $stateParams ){
//send state param: receiptId to service and get data
var receiptAPIresponse = receiptAPI.query({receiptId:$stateParams.receiptId});
return receiptAPIresponse;
}
},
controller: ['$scope', '$stateParams', 'receipt',
function ( $scope, $stateParams, receipt) {
$scope.receipt = receipt;
}]
},
这是我的资源服务,它从API请求数据:
var receiptAPIservice = angular.module('receiptAPIservice', ['ngResource']);
receiptAPIservice.factory('receiptAPI', ['$resource',
function($resource){
return $resource('http://myapi.url/api/receipts/:rId', {
rId: '@receiptId',
}, {
query: {
method:'GET',
headers:{
'session-id':'rest-session-id-forconsumer-consumerid2',
'Content-Type':'application/json',
'Accept':'application/json'
},
}
});
}]);
这一切都有效,除了服务正在请求URL:
http://myapi.url/api/receipts?receiptId=81
当我希望将网址格式化为:
http://myapi.url/api/receipts/81
模板替换似乎无法正常工作,因为它应该在以下文档中指定它:http://docs.angularjs.org/api/ngResource/service/$resource
有人可以告诉我我错过了什么吗?
答案 0 :(得分:0)
这一行:
var receiptAPIresponse = receiptAPI.query({receiptId:$stateParams.receiptId});
需要改为:
var receiptAPIresponse = receiptAPI.query({rId:$stateParams.receiptId});
请注意将receiptId
重命名为rId
属性。您需要确保参数名称与URL模板中的占位符匹配。