我想使用' ng-model'以数组的形式放置动态复选框的值(不是布尔值true和false)。以与使用' name'属性。此数组现在放入JSON对象中。
<td>
<span ng-repeat="operation in operations_publish">
<input type="checkbox" name="operations" ng-model="operations" value="{{operation}}"/>
{{operation}}
</span>
</td>
以下是我发布JSON对象的函数:
$scope.send = function() {
console.log("test");
var dataObj = {
"operationType" : $scope.operationType,
"conceptModelID" : $scope.conceptID,
"requestor" : $scope.requestor,
"status" : "new",
"requestDateTime" : null,
"lastExecutedDateTime" : null,
"completedDateTime" : null,
"instructions" : $scope.operations
};
console.log(dataObj);
console.log(dataObj.instructions);
var response = $http.post('PostService', dataObj);
response.success(function(data, status, headers, config) {
$scope.responseData = data;
});
response.error(function(data, status, headers, config) {
alert("Exception details: " + JSON.stringify({
data : data
}));
});
但是&#39; dataObj.instructions&#39;我运行代码时未定义。请说明这是否是正确的做法以及我在这里缺少的。
答案 0 :(得分:3)
您必须将每个输入绑定到不同的值。目前,所有这些都通过@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
/**
* Do not modify addRestResourceClasses() method.
* It is automatically populated with
* all resources defined in the project.
* If required, comment out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(beerrestful.BeerResources.class);
}
绑定到范围中的字段operations
。
我建议您在控制器中创建一个数组ng-model="operations"
,如下所示:
operations
然后,您可以将复选框绑定到此数组中的相应索引:
$scope.operations = new Array($scope.operations_publish.length);
这将为您提供所有已检查索引的<span ng-repeat="operation in operations_publish">
<input type="checkbox"
name="operations"
ng-model="operations[$index]"
value="{{operation}}"/>
{{operation}}
</span>
数组。如果您希望将所选值作为数组中的字符串,则可以像这样收集它们:
true
检查this fiddle以获取完整代码。
答案 1 :(得分:0)
<input type="checkbox" ng-model="cbValues[$index]"
ng-repeat="value in cbValues track by $index" />
答案 2 :(得分:0)
根据您在那里列出的示例,您已将ng-model
绑定到表达式operations
,但是您需要将其绑定到单个迭代器operation
(来自){{1 }}
然后,您可以在dataLog对象中设置该数据:
ng-repeat="operation in operations_publish"
默认情况下,角度数据绑定是多向的:
var dataObj = {
"operationType" : $scope.operationType,
"conceptModelID" : $scope.conceptID,
"requestor" : $scope.requestor,
"status" : "new",
"requestDateTime" : null,
"lastExecutedDateTime" : null,
"completedDateTime" : null,
"instructions" : $scope.operations_publish
};
=&gt;通过迭代器operations_publish
operation
=&gt;通过operation