1

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'}
    });
});

ご協力いただきありがとうございます。

4

1 に答える 1

1

このようにリソースを投稿してみてください

var newPlayer = {
        teamId: $scope.newPlayer.teamId,
        position: $scope.newPlayer.position,
        lastName: $scope.newPlayer.lastName,
        firstName: $scope.newPlayer.firstName
    };

Players.save(newPlayer);
于 2013-08-15T02:31:05.980 に答える