我有一个使用 Spring MVC Rest API 的相当简单的 AngularJS 应用程序。
我可以查询 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'}
});
});
感谢您提供任何帮助。