0

前端代码

    <button data-ng-click="deleteRec()" id="Delete">delete</button>
        var app = angular.module('myApp', ['ngResource']);
 app.controller('UserController', ['$scope', '$resource',function($scope,$resource) 
    {
             $scope.deleteRec = function()
                     {
                        User = $resource(
                                'delete/:username',
                                 {method:'DELETE', params: {username: '@username'}});
                        User.delete({username: $scope.myform.username}).then(function successCallback(response) 
                        {
                            $scope.Message = response;
                        }, function errorCallback(response) {

                        });

                        $scope.myform.username = "";
                        $scope.myform.phone="";
                        $scope.myform.email="";
                        $scope.myform.address="";
                        $scope.myform.password=""; 
                      };
}]);

控制器代码

@RequestMapping(value="/delete/{username}")
    public @ResponseBody String delete(@PathVariable("username") String username) 
    {
        String user=retrievedataservice.delete(username);
        return null;
    }

错误是这样的 TypeError: User.delete(...).then is not a function 实际上,User 是一个对象和 delete() 方法,用于在 Spring Controller 方法 delete() 中传递参数。

4

1 回答 1

1

正如我在文档中提到的,

User.delete 是一个类操作,返回一个空实例(具有以下附加属性之一)

$promise:创建此实例或集合的原始服务器交互的承诺。

所以你需要调用$promise实例的。

 var app = angular.module('myApp', ['ngResource']);
 app.controller('UserController', ['$scope', '$resource',function($scope,$resource) 
    {
             $scope.deleteRec = function()
                     {
                        User = $resource(
                                'delete/:username',
                                 {method:'DELETE', params: {username: '@username'}});
                        User.delete({username: $scope.myform.username}).$promise.then(function successCallback(response) 
                        {
                            $scope.Message = response;
                        }, function errorCallback(response) {

                        });

                        $scope.myform.username = "";
                        $scope.myform.phone="";
                        $scope.myform.email="";
                        $scope.myform.address="";
                        $scope.myform.password=""; 
                      };
}]); 
于 2018-12-11T08:01:47.093 回答