我使用相同的html页面来创建新的rma并编辑旧的rma。我试图在AngularJs中实现路由,但是当选择从rma列表列表中编辑旧对象时它不起作用。
路由
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/rma', {
templateUrl: 'views/rma.html',
controller: 'RmaCtrl'
})
.when('/rma/:rmaId', {
templateUrl: 'views/rma.html',
controller: 'RmaCtrl'
})
.when('/rma-list', {
templateUrl: 'views/rma-list.html',
controller: 'RmaListCtrl'
})
RMA列表控制器
$scope.updateRma = function (rmaId) {
console.log("updateRma---->" +rmaId);
$location.path('/rma/'+rmaId);
console.log("2. ABSURL---->" +$location.absUrl());
// ABSURL---->http://localhost:8383/RmaClient/app/index.html#/rma-detail/%5Bobject%20Object%5D
};
RMA-list.html
<td><a ng-click="updateRma(rma.rmaId)" class="btn btn-small btn-success">edit</a></td>
rma controller rma.js
angular.module('rmaClientApp')
.controller('RmaCtrl',
function ($scope, $location, rmaService, $routeParams) {
console.log("------RmaCtrl------" +$routeParams.rmaId);
//When creating a new one, should be 0
$scope.editMode = ($routeParams.rmaId === "0");
if ($scope.editMode) {
console.log("----Creating a new RMA-----");
$scope.rma = rmaService.get({id: $routeParams.rmaId});
//console.log("$scope.rma --->" +$scope.rma);
$scope.rmaService = rmaService;
} else {
console.log("----Editing an old RMA rmaId: "+$routeParams.rmaId );
var rma = $scope.rma;
$scope.rma = rmaService.get({id: $routeParams.rmaId});
}
rmaService
angular.module('rmaServices', ['ngResource'])
.factory('rmaService', ['$resource',
function ($resource) {
return $resource(
'http://localhost:8080/RmaServer/webresources/com.sako.rma.rma/:rma:rmaId',
{},
{
update: { method: 'PUT', params: {id: 'rmaId'} }
});
}]);
问题
在服务器端,REST服务工作正常,但在客户端尝试编辑rma 7时,我只得到一个空白页面和错误消息。此链接返回相关的rma whit ID = 7,链接为:
http://localhost:8080/RmaServer/webresources/com.ako.rma.rma/7
当我尝试从网页中选择相同的rma时,链接如下:
http://localhost:8383/RmaClient/app/index.html#/rma/7
错误
------RmaCtrl------7 (12:28:34:557)
在app / scripts / controllers / rma.js:13
错误:[$ resource:badcfg]操作get
的资源配置出错。包含一个对象但得到一个数组的预期响应
http://errors.angularjs.org/1.3.15/ $资源/ badcfg P0 = GET&安培; P1 =对象&安培; P2 =阵列
错误是这样的: $ scope.rma = rmaService.get({id:$ routeParams.rmaId});
答案 0 :(得分:1)
您的资源网址应为
http://localhost:8080/RmaServer/webresources/com.sako.rma.rma/:id'
然后
http://localhost:8080/RmaServer/webresources/com.sako.rma.rma/:rma:rmaId'
只传递一个参数rmaId
angular.module('rmaServices', ['ngResource'])
.factory('rmaService', ['$resource',
function ($resource) {
return $resource(
'http://localhost:8080/RmaServer/webresources/com.sako.rma.rma/:id',
{id:"@id"},
{
update: { method: 'PUT', params: {id: 'rmaId'} }
});
}]);