我有产品清单,每个产品都有学生名单。 我有产品JSON来显示;在该产品JSON中我是另一个学生JSON:
[{"CustId":7191,"CFirstName":"Kynan"},{"CustId":29689,"CFirstName":"Pete"},{"CustId":29690,"CFirstName":"Gina"},{"CustId":29692,"CFirstName":"Jo"}]
我想为每个学生和每个产品添加复选框。
以下是我为每种产品所做的事情:
<span ng-repeat="customer in productVM.product.Customers">
<label class="checkbox-inline" style='margin-left:0px !important; margin-right: 10px !important; '>
<input class="options" ng-model="customer.CustId" type='checkbox' name="selectedStudent[]"
value="{{customer.CustId}}" ng-checked="selection.indexOf(customer.CFirstName) > -1" ng-click="toggleSelection(customer.CFirstName)">
{{customer.CFirstName}}
</label>
</span>
角度代码:
$scope.selection = [];
$scope.toggleSelection = function toggleSelection(customerName) {
var idx = $scope.selection.indexOf(customerName);
// is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
$scope.selection.push(customerName);
}
};
我有两个问题: 1.每当我点击产品的任何复选框时;所有产品的类似复选框都会被选中(当我取消选中时类似)。 2.我如何获得所选复选框的值。
答案 0 :(得分:1)
id
元素上定义唯一的input
属性。 ng-model
https://docs.angularjs.org/api/ng/directive/ngModel - 默认使用复选框不需要值...只需在客户对象中定义新字段(例如customer.checked)并将其绑定到{{1指令。
ng-model
答案 1 :(得分:0)
这对我有用:
<label class="checkbox-inline" style='margin-left:0px !important; margin-right: 10px !important; '>
<input class="options" ng-model="selection[productVM.product.Id].customers[customer.CustId]" type='checkbox' name="selectedStudent[]"
value="{{customer.CustId}}">
{{customer.CFirstName}}
</label>
来源:
http://stackoverflow.com/questions/27148320/how-to-use-checkbox-with-nested-ng-repeat-in-angularjs