如何获得与刚刚从选择复选框的数值数组? [英] How to get an Array with just the values from selected checkboxes?

查看:100
本文介绍了如何获得与刚刚从选择复选框的数值数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何绑定复选框在我的模型阵列,使阵列始终包含对应于所选复选框只值?

例如,而不是:

 组:{名1:真的,NAME2:假的,NAME3:真正}

我需要:

 组:[名称1,NAME3]

我经历了很多的问题,浏览和一些答案使用建议 NG-真正获取自定义值,但这并不能解决问题,因为那时我得到:

 组:{名1:CustomvalueIset,名2:CustomValueIset,NAME3:CustomValueIset'}

我目前的code是这样的:

 < D​​IV CLASS =表单组>
   <标签类=控制标签为=群基团< /标签>< BR />
   <标签类=复选框-BR数据-NG-重复=成群组>
       <输入类型=复选框ID =组数据-NG-模式=model.groups [group.name]>
       {{group.name}}
   < /标签>
< / DIV>


解决方案

有在AngularJS为复选框添加/从阵列中移除值没有原生支持。不能有,因为窗体控件模型 - 视图绑定由 ngModel 管理​​,这就需要控制(输入)将其值绑定到单独分配前pression(所以没有办法让 ngModel 删除的当复选框未选中的值)。

不要介意未定义

您可以通过解决这个 model.groups 阵列,并使用以下操作code

 <标签NG重复=成群组>
    <输入类型=复选框NG模型=model.groups [$指数]NG-真值=group.nameNG-假值=未定义>
    ...
< /标签>

,然后筛选出未定义使用阵列时秒。

计算属性

您也可以直接指定布尔真正 / 值一个对象,并设置一个 $手表的对象,自动更新您的阵列上。

 <输入类型=复选框NG模型=model.groupFlags [group.name]>

+

  VAR组= $ scope.model.groups = [];
$范围。$腕表('model.groupFlags',函数(标志){
    groups.length = 0;
    对(在国旗VAR键){
        groups.push(键);
    }
},真正的);


原来的答复(问题是不清楚):

文档建议 NG-真值 NG-假值应该只是罚款:

 <输入类型=复选框NG模型=...吴真值=group.nameNG-假值='' >

How can I bind checkboxes to an array in my model, so that the array always contains just the values corresponding to the selected checkboxes?

For example, instead of:

"groups": { "name1": true, "name2": false, "name3": true }

I need to get:

 "groups": [ "name1", "name3" ]

I browsed through a lot of questions and some answers suggested using ng-true for getting custom values, but that does not solve the problem, because then I get:

"groups": { "name1": 'CustomvalueIset', "name2": 'CustomValueIset', "name3": 'CustomValueIset' }

My current code looks like this:

<div class="form-group">
   <label class="control-label" for="group">Groups</label><br/>
   <label class="checkboxes-br" data-ng-repeat="group in groups">
       <input type="checkbox" id="group" data-ng-model="model.groups[group.name]">
       {{group.name}}
   </label>
</div>

解决方案

There is no native support in AngularJS for checkboxes adding/removing values from an array. There cannot be, because form control model-view binding is managed by ngModel, which requires the control (input) to bind its value to a separate assignable expression (so there's no way to make ngModel delete a value when the checkbox is unchecked).

Don't Mind undefined

You can either solve this by making model.groups an Array and using doing the following code:

<label ng-repeat="group in groups">
    <input type="checkbox" ng-model="model.groups[$index]" ng-true-value="group.name" ng-false-value="undefined">
    ...
</label>

and then filter out the undefineds when using the array.

Computed Property

Or you can just assign the boolean true/false values to an object and set a $watch on that object that automatically updates your array.

<input type="checkbox" ng-model="model.groupFlags[group.name]">

+

var groups = $scope.model.groups = [];
$scope.$watch('model.groupFlags', function (flags) {
    groups.length = 0;
    for (var key in flags) {
        groups.push(key);
    }
}, true);


Original answer (the question was unclear):

The documentation suggests that ng-true-value and ng-false-value should work just fine:

<input type="checkbox" ng-model="..." ng-true-value="group.name" ng-false-value="''">

这篇关于如何获得与刚刚从选择复选框的数值数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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