简体   繁体   English

如何获取 Angular (10+) 中的所有复选框值?

[英]How to get all checkboxes values in Angular (10+)?

I retrieve from the database what options should I show for all companies (only for showing purposes), this is my model:我从数据库中检索应该为所有公司显示哪些选项(仅用于显示目的),这是我的 model:

 export class OpcionesModel {
        name: string;
        isActive: boolean;
        id?: string;
        
 }

Then, I show all the options that are available (active):然后,我显示所有可用的选项(活动):

<div class="row" *ngFor="let option of optionsArray; let i=index">
        <div class="col-4">
            <h5>{{option.name | titlecase}}</h5>
          
        </div>
        <div class="col">
            <input type="checkbox">

        </div>
        
    </div>

What I want to do is to get all option's names and if its checkbox is checked or not and store that in an array, eg:我想要做的是获取所有选项的名称以及是否选中了它的复选框并将其存储在一个数组中,例如:

[{'option 1', true}, {'option 2'}, false}, {'option 3', false}...]. [{'option 1', true}, {'option 2'}, false}, {'option 3', false}...]。

EDIT: I've created an array: checkes: boolean[] =[];编辑:我创建了一个数组: checkes: boolean[] =[];

if I do this <input type="checkbox" [(ngModel)]="checkes[i]"> , I can get the checkboxes value but I need the option name too.如果我这样做<input type="checkbox" [(ngModel)]="checkes[i]"> ,我可以获得复选框值,但我也需要选项名称。

Thanks !!谢谢 !!

<label>
    <input type="checkbox" name="isActive"  [(ngModel)]="option.isActive">
    {{ option.name }}
</label>

taken from here: https://scotch.io/tutorials/how-to-deal-with-different-form-controls-in-angular-2取自这里: https://scotch.io/tutorials/how-to-deal-with-different-form-controls-in-angular-2

In your ts file, Your optionsArray array will be binded and the checked values will be updated when it changes在您的 ts 文件中,您的optionsArray数组将被绑定,并且检查的值将在更改时更新

If you want to map it to an array with a different structure you can use the map method:如果您想将 map 转换为具有不同结构的数组,您可以使用 map 方法:

this.optionsArray.map(oa=> {return { myOptionName:oa.name, active:oa.isActive} })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM