如何从角材料复选框中获取值 [英] How to get the value from angular material checkbox

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

问题描述

我有一个复选框列表:

    <section *ngFor="let item of list">
      <mat-checkbox [checked]="item.value" (click)="toggle(_checkbox_value_here_)">{{item.key}}</mat-checkbox>
    </section>

我需要将 checkbox 值 (_checkbox_value_here_) 传递给我的函数,你会怎么做?

I need to pass the checkbox value (_checkbox_value_here_) to my function, how would you do that?

我可以看到这个相关的问题 How to get checkbox data in angular材料 但真的有办法在不使用 ngModelreactive forms 的情况下实现吗?

I can see this related question How to get checkbox data in angular material but really is there a way go achieve that without using ngModel and reactive forms?

推荐答案

复选框上的点击事件只是原生的点击事件,对复选框本身一无所知.使用更改事件或直接获取 MatCheckbox 实例的句柄(例如使用@ViewChildren)将是推荐的方法.

The click event on the checkbox is just the native click event, which doesn't know anything about the checkbox itself. Using the change event or just getting a handle on the MatCheckbox instance directly (e.g. with @ViewChildren) would be the recommended approach.

(c) https://github.com/angular/material2/issues/13156#issuecomment-427193381

<section *ngFor="let item of list">
  <mat-checkbox [checked]="item.value" (change)="toggle($event)">{{item.key}}</mat-checkbox>
</section>

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

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