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

时间:2016-02-18 09:00:17

标签: php angularjs

如何将多个复选框值保存到数据库中我已编写下面的代码我无法理解如何将$ scope.selection数组传递给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);
      }
    };
  };

提前感谢您的早期回复将非常高兴

1 个答案:

答案 0 :(得分:2)

当您调用$ 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) {
}