0

前端代码

      var app = angular.module('myApp', ['ngResource']);
         app.controller('UserController', ['$scope', '$resource',function($scope,$resource) 
            {
         $scope.deleteRec = function(username)
                 {
                    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="";
                  };

         }]);
<table border="1" width="50%" height="50%"> 
    <tr><th>user_name</th><th>phone</th><th>email</th><th>delete</th></tr>
     <tr data-ng-repeat="user in usernames">
     <td><span data-ng-bind="user.username"></span></td>
      <td><span data-ng-bind="user.phone"></span></td>
       <td><span data-ng-bind="user.email"></span></td>
        <td><button data-ng-click="deleteRec(user.username)">delete</button>
       </tr>   
   </table>   

弹簧控制器代码

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

如何通过 angularjs 请求删除特定记录。Angularjs不将参数发送到spring控制器

4

1 回答 1

0

在 angularjs 中,执行一次 API 调用:

$http({
  method: 'DELETE',
  url: 'delete/:username,
  data: {
    username: $scope.myform.username
  },
  headers: {
    'Content-type': 'application/json;charset=utf-8'
  }
}).then(function(response) {
    $scope.Message = response

});

在春天:

public String delete(@PathVariable('username') String username){}

@PathVariable用于从 URI 模板访问参数

或改为使用,

public String delete(@RequestParam String username){}
于 2018-12-11T11:24:46.080 回答