我有一个相当简单的AngularJS应用程序正在使用Spring MVC Rest API。
我可以查询API。但是我在创建对象时遇到了麻烦。我收到以下错误,但无法弄清楚原因。我看过类似的问题,但我的API正在返回创建的对象,而且我可以告诉我的注射是正确的。
这是来自javascript控制台的堆栈:
TypeError: Object #<Resource> has no method 'save'
at Object.TeamListCtrl.$scope.addPlayer(http://localhost:8000/app/js/controllers.js:17:13)
at elementFns (http://localhost:8000/app/lib/angular/angular.js:6365:19)
at Object.$get.Scope.$eval (http://localhost:8000/app/lib/angular/angular.js:8057:28)
at Object.$get.Scope.$apply (http://localhost:8000/app/lib/angular/angular.js:8137:23)
at Object.ng.config.$provide.decorator.$delegate.__proto__.$apply (http://localhost:8000/app/index.html:855:30)
at HTMLFormElement.ngIncludeDirective.restrict (http://localhost:8000/app/lib/angular/angular.js:13159:11)
at event.preventDefault (http://localhost:8000/app/lib/angular/angular.js:1992:10)
at Array.forEach (native)
at forEach (http://localhost:8000/app/lib/angular/angular.js:130:11)
at HTMLFormElement.eventHandler (http://localhost:8000/app/lib/angular/angular.js:1991:5)
这是我的控制器:
function TeamListCtrl($scope, Shared, Teams, Players) {
$scope.teams = Teams.query();
$scope.addPlayer = function() {
var newPlayer = new Players({
position: $scope.newPlayer.position,
lastName: $scope.newPlayer.lastName,
firstName: $scope.newPlayer.firstName
});
newPlayer.save({teamId: $scope.newPlayer.teamId});
};
}
TeamListCtrl.$inject = ['$scope', 'Shared', 'Teams', 'Players'];
这是我的服务:
var teamsService = angular.module('teamsService', ['ngResource']);
teamsService.factory('Players', function($resource){
return $resource('http://localhost\\:8080/api/teams/players/:teamId', {teamId:'@teamId'}, {
save: {method:'POST'}
});
});
感谢您提供任何帮助。
答案 0 :(得分:1)
您可以尝试发布资源,例如
var newPlayer = {
teamId: $scope.newPlayer.teamId,
position: $scope.newPlayer.position,
lastName: $scope.newPlayer.lastName,
firstName: $scope.newPlayer.firstName
};
Players.save(newPlayer);