使用angularjs保存多个复选框值数据库

时间:2022-12-02 17:41:29

How to save multiple checkbox value into database i have written a code below i cant understand how to pass the $scope.selection array into url please see below code and suggest me

如何将多个复选框值保存到数据库中我在下面写了一个代码,我不知道如何传递$范围。选择数组到url请见下面的代码并建议我

<tr ng-repeat="result in results">
 <td <input type="checkbox" name="selectedadd" ng-model="addvalue" value="{{result.user_id}}" ng-checked="selection.indexOf(result.user_id) > -1" ng-click="toggleSelection(result.user_id)"> {{result.name}}</td>
</tr>
<td  ng-click="linkToSelectedPlayer()" value="{{result.user_id}}"> <i class=""></i> Selected Add</a></td>

$scope.selection=[];  

    $scope.linkToSelectedPlayer = function(){
    console.log($scope.selection);

   //Getting response with $scope.selection

   //Array[156,355,665,666]

    $http.post("v1/xyz/"+$scope.user.user_id+"/abc/"+$scope.selection)
        .success(function (response){

          $location.path('/home/'); 

        }).error(function (data) {
          alert(“invalid”);
        });

    // toggle selection for a given employee by id
    $scope.toggleSelection = function toggleSelection(userid) {
      var idx = $scope.selection.indexOf(userid);

      // is currently selected
      if (idx > -1) {
        $scope.selection.splice(idx, 1);
      }

      // is newly selected
      else {
        $scope.selection.push(userid);
      }
    };
  };

Thanking you in advance early reply will be highly apprecaited

提前感谢您的回复,我们将不胜感激

1 个解决方案

#1


2  

When you call the $http.post() function you can pass 2 arguments: the URL and the post data like this:

当您调用$http.post()函数时,您可以传递两个参数:URL和这样的post数据:

$http.post(URL, data)

More over .success() and .error() functions are deprecated, you should use .then()

对于更多的.success()和.error()函数,我们不建议使用。

$http.post(URL, arrayObject)
.then(function successCallback(response) {
}, 
function errorCallback(response) {
}

Then in your server side controller you can get the post data like this (sorry it's java code, I don't know about php)

然后在服务器端控制器中你可以得到这样的post数据(不好意思,是java代码,我不知道php)

@RequestMapping( value="/URL",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public void yourFunction(@RequestBody DataDto dataDto) {
}

#1


2  

When you call the $http.post() function you can pass 2 arguments: the URL and the post data like this:

当您调用$http.post()函数时,您可以传递两个参数:URL和这样的post数据:

$http.post(URL, data)

More over .success() and .error() functions are deprecated, you should use .then()

对于更多的.success()和.error()函数,我们不建议使用。

$http.post(URL, arrayObject)
.then(function successCallback(response) {
}, 
function errorCallback(response) {
}

Then in your server side controller you can get the post data like this (sorry it's java code, I don't know about php)

然后在服务器端控制器中你可以得到这样的post数据(不好意思,是java代码,我不知道php)

@RequestMapping( value="/URL",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public void yourFunction(@RequestBody DataDto dataDto) {
}