【发布时间】:2015-06-11 20:39:01
【问题描述】:
我正在使用相同的 html 页面来创建新的 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 控制器 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/$resource/badcfg?p0=get&p1=object&p2=array
错误线是这样的:$scope.rma = rmaService.get({id: $routeParams.rmaId});
【问题讨论】:
-
您可以尝试将 href="#" 添加到锚标记吗?还要添加参数: {id: 'rmaId'}, isArray: false 以便它查找对象而不是数组。
-
对于编辑,您也可以调用 get。你应该调用类似 rmaService.update() 的方法。
-
尝试调用 update() 时--->加载资源失败:服务器响应状态为 405(不允许方法)(12:58:36:638 | 错误,网络)在localhost:8080/RmaServer/webresources/com.sako.rma.rma?id=rmaId
-
paje007 - 我也在使用相同的服务来获取 rmas 列表。我尝试了完全相同的错误消息。更新:{方法:'PUT',参数:{id:'rmaId'},isArray:假}
标签: angularjs angularjs-service angularjs-routing