Angular ng-Repeat with checkboxes 存储选项
Angular ng-Repeat with checkboxes storing choices
Angular 1.4
我有一个带有复选框的简单 ng-repeat
。目标是将 customer.Id
添加到 $scope.holdsChosenCustomerIds
中。我需要 $scope.holdsChosenCustomerIds
像 [3,78,445,789] 这样的哑数组,基本上是用户选择的任何内容。
$scope.holdsChosenCustomerIds= [];
<div ng-repeat="customer in customers">
<input type='checkbox' ng-model="holdsChosenCustomerIds[$index]==???" ????????? />
</div>
卡在了这里
只需使用 ng-repeat 的实例作为模型
像这样
<div ng-repeat="customer in customers">
<input type='checkbox' ng-model="customer.isChecked" />
</div>
您可以从$scope.customers
获取选中项目的信息
如果您需要检查真实项目
你可以这样试试
$scope.holdsChosenCustomerIds= $scope.customers.filter(function(x){ return x.isChecked; });
你可以在你的 HTML 中这样写 ng-change
:
<div ng-repeat="customer in customers">
<input type='checkbox' ng-model="customer.selected" ng-change="selectUnselect()" />
</div>
在你的控制器中:
$scope.selectUnselect = function() {
var customer = this.customer;
if (customer.selected) {
$scope.holdsChosenCustomerIds.push(customer.id);
} else {
var idIndex = $scope.holdsChosenCustomerIds.indexOf(customer.id);
if (idIndex > -1) {
$scope.holdsChosenCustomerIds.splice(idIndex, 1);
}
}
};
Angular 1.4
我有一个带有复选框的简单 ng-repeat
。目标是将 customer.Id
添加到 $scope.holdsChosenCustomerIds
中。我需要 $scope.holdsChosenCustomerIds
像 [3,78,445,789] 这样的哑数组,基本上是用户选择的任何内容。
$scope.holdsChosenCustomerIds= [];
<div ng-repeat="customer in customers">
<input type='checkbox' ng-model="holdsChosenCustomerIds[$index]==???" ????????? />
</div>
卡在了这里
只需使用 ng-repeat 的实例作为模型
像这样
<div ng-repeat="customer in customers">
<input type='checkbox' ng-model="customer.isChecked" />
</div>
您可以从$scope.customers
如果您需要检查真实项目
你可以这样试试
$scope.holdsChosenCustomerIds= $scope.customers.filter(function(x){ return x.isChecked; });
你可以在你的 HTML 中这样写 ng-change
:
<div ng-repeat="customer in customers">
<input type='checkbox' ng-model="customer.selected" ng-change="selectUnselect()" />
</div>
在你的控制器中:
$scope.selectUnselect = function() {
var customer = this.customer;
if (customer.selected) {
$scope.holdsChosenCustomerIds.push(customer.id);
} else {
var idIndex = $scope.holdsChosenCustomerIds.indexOf(customer.id);
if (idIndex > -1) {
$scope.holdsChosenCustomerIds.splice(idIndex, 1);
}
}
};