AngularJS将复选框的值添加到数组

问题描述:

我有此代码:

<tr ng-repeat="doc in providers">      
  <td><input type="checkbox" ng-true-value="{{doc.provider.Id}}" ng-false-value="" ng-model="ids"></td> 
</tr>

{{ids}}

我想获取数组中复选框的值

i want to get the values of the checkboxes on an array

ng-true-value仅接受字符串,因此您需要使用解决方法.一段时间以来,这一直是功能请求.在此期间,您可以执行以下操作:

ng-true-value only accepts strings so you'll need to use a workaround. This has been a feature request for some time. In the meantime, you can do this:

在控制器中创建一个ids对象,例如:

Create an ids object in the controller like:

$scope.ids = {};

并更改ng-model以引用该对象中的键.您可以使用默认的true/false复选框值:

and change ng-model to reference a key in that object. You can use the default true/false checkbox values:

<td><input type="checkbox" ng-model="ids[doc.provider.Id]"></td>

然后,您可以遍历ids中的键以检查true.

Then you can loop over the keys in ids checking for true.

这是一个小提琴