AngularJS将复选框的值添加到数组 [英] AngularJS add value of checkboxes to an array

查看:356
本文介绍了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.

这是一个小提琴

这篇关于AngularJS将复选框的值添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆