Angular2如何获取所有选中的复选框 [英] Angular2 How to get all selected check boxes

查看:1203
本文介绍了Angular2如何获取所有选中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在研究一个Angular2应用程序。
我有一张表,其中每个记录代表学生,并且包括一个复选框

So I am working on an Angular2 application. I have a table where each record represent a student and includes a checkbox

<input class="mycheckbox" type="checkbox" [value]='student.StudentId'>

在某些时候,用户将点击需要获取所有选定复选框的值的按钮

At some point user will click on a button that needs to get the value of all selected check-boxes.

我不知道我应该解决这个问题。

I am not sure who should I address this.

一个想法是,每个学生都会有已检查或未检查的值。这必须通过双向绑定来完成。

One idea is that each student will have a value of checked or not. And this has to be done through two-way bindings.

然而,这意味着每次你必须通过所有的学生

However this will imply that each time u have to go through all students

这是最好的可用选项吗?
有没有匹配以下JQuery的东西:

Is this the best available option ? And is there something that matches the following JQuery:

$('.mycheckbox:checked').each(function(){


推荐答案

我最近回答了一个类似的问题: http://stackoverflow.com/a/34142740/215945

I recently answered a similar question: http://stackoverflow.com/a/34142740/215945

您可以在模板中执行以下操作:

You could do the following in your template:

<input class="mycheckbox" type="checkbox" [(ngModel)]="student.selected">{{student.StudendId}}

然后,为所选学生做一些事情:

Then, to do something with the selected students:

this.students.filter(_ => _.selected).forEach(_ => { ... })

这篇关于Angular2如何获取所有选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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