【问题标题】:Handle change on checkbox Angular2处理复选框Angular2上的更改
【发布时间】:2017-11-22 16:10:25
【问题描述】:

我有复选框,我想在上面使用(change)。默认为“选中”,但单击后我想要清除输入文本“激活密钥”。重新检查后,我想生成 guid 并再次添加到输入中。如何获取复选框是否被选中?

<div class="checkbox">
    <label>
       <input type="checkbox" checked (change)="handleChange($event)" > 
           Generate key
    </label>
</div>

TS

handleChange(e) {
    var isChecked = e.isChecked;
    if (isChecked) {
        this.gatewayForm.patchValue({ 'activationKey': this.guid() });
    }
    else {
       this.gatewayForm.controls['activationKey']
           .setValue('', { onlySelf: true });
    }
}

【问题讨论】:

    标签: angular checkbox angular2-forms


    【解决方案1】:

    您可以通过e.target.checked获取复选框状态

    handleChange(e) {
      var isChecked = e.target.checked;
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 2017-02-02
      • 2012-02-29
      • 2016-08-30
      • 2021-07-17
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多