【问题标题】:How to return boolean values for checkbox checked and unchecked in ANGULAR Material 6如何在 ANGULAR Material 6 中为选中和未选中的复选框返回布尔值
【发布时间】:2019-03-01 11:32:24
【问题描述】:

我想要得到的概念是 在选择复选框时,我希望将值返回为 TRUE 和 在取消选择同一个复选框时,我希望将值返回为 False

这是我的 HTML 代码

<div class="form-group m-form__group row">
                                        <div class="col-lg-4">
                                            <mat-checkbox class="example-margin" type= checkbox value = "true" formControlName ="rti_cover" [checked] = "true">RTI</mat-checkbox>
                                        </div>
                                        <div class="col-lg-4">
                                            <mat-checkbox class="example-margin" type= checkbox value = "true" formControlName ="nildep_cover" [checked] = "true" >Nil Dep</mat-checkbox>
                                        </div>
                                    </div>

【问题讨论】:

    标签: checkbox angular6 angular-material-6


    【解决方案1】:

    您可以通过绑定到(更改)EventEmitter 来获取复选框的值。您可以在此处的文档中看到这一点:https://material.angular.io/components/checkbox/api

    在您的组件中,您可以创建一个侦听(更改)事件的函数,并从您的模板中传递该事件。

    checkboxChanged(event) {
      console.log(event.checked);
    }
    

    该事件包含属性“checked”,它是一个布尔值,表示该框是否被选中。

    然后您可以在模板中绑定到事件:

    <mat-checkbox (change) = "checkboxChanged($event)">Check me!</mat-checkbox>
    

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 2013-10-04
      • 1970-01-01
      • 2012-02-23
      • 2012-12-13
      • 1970-01-01
      • 2023-03-16
      • 2017-12-17
      • 2011-08-15
      相关资源
      最近更新 更多