我对angularjs很陌生。现在我的一个视图中有一个复选框
<input id="{{fields[3].name}}" type="checkbox" value="{{fields[3].name}}" ng-checked="selection.indexOf(fields[3].name) > -1" ng-click="toggleSelection(fields[3].name)" class="col-xs-1" />
在弹簧控制器(如下所示)中,如何检查复选框是否已选中?
@RequestMapping(value = "/test/{id}/accept", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
public @ResponseBody ResponseBean acceptData(
@PathVariable("id") String id,
@RequestBody AcceptPayload payload, Model model,
HttpServletRequest request, HttpServletResponse response) {
....
}
任何代码示例都将有所帮助
最佳答案
您需要将检查的数据从您的网页发送到控制器。
该网页将具有ng-repeat,如下所示。变量列表包含json数组:
<tr ng-repeat="obj in list">
<td>
<input id="{{obj.name}}" type="checkbox" value="{{obj.name}}"
checklist-model="checkboxes" ng-checked="selection.indexOf(obj.name) > -1"
ng-click="toggleSelection(obj.name)" />
</td>
</tr>
toggleSelection()函数将获取所选obj.name的数组,并将其添加到变量数组** selectedItems **中:
$scope.selectedItems = [];
$scope.toggleSelection = function toggleSelection(name) {
var idx = $scope.selectedItems.indexOf(name);
// is currently selected
if (idx > -1) {
$scope.selectedItems.splice(idx, 1);
}
// is newly selected
else {
$scope.selectedItems.push(name);
}
};
我在网页中点击控制器的方式如下:
$scope.execute = function() {
$http({
method : 'GET',
url : 'trigger',
params : {
selectedItems : $scope.selectedItems
}
}).success(
function(data, status, headers, config) {
}).error(
function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
控制器中的方法如下所示:
@RequestMapping(value = "/trigger", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody String trigger(@RequestParam("selectedItems") List<String> selectedItems) throws NumberFormatException, Exception {
// Your method
}
您可以根据需要将方法从GET更改为POST。