2015-12-01 54 views
0

Angular 1.4Angular ng - 用复选框重复存储选项

我有一个简单的ng-repeat带复选框。目标是将customer.Id添加到$scope.holdsChosenCustomerIds中。我需要$scope.holdsChosenCustomerIds像[3,78445789]这样的哑数组,基本上无论用户选择什么。

$scope.holdsChosenCustomerIds= []; 

<div ng-repeat="customer in customers"> 
    <input type='checkbox' ng-model="holdsChosenCustomerIds[$index]==???" ????????? /> 
</div> 

停留在这一点上

回答

1

只需使用NG重复的实例作为模型

喜欢这个

<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; }); 
0

你可以这样写在你的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); 
     } 
    } 
};