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


save multiple checkbox values database using angularjs

如何将多个复选框值保存到数据库中 我在下面写了一个代码,我不明白如何将 $scope.select 数组传递到 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);
      }
    };
  };

提前感谢你早点回复会得到高度赞赏

当你调用 $http.post() 函数时,你可以传递 2 个参数:URL 和帖子数据,如下所示:

$http.post(URL, data)

此外,不推荐使用 .success() 和 .error() 函数,您应该使用 .then()

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

然后在你的服务器端控制器中你可以像这样获取帖子数据(对不起,这是java代码,我不知道php)

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