Italik
Italik

Reputation: 696

Handle change on checkbox Angular2

I have checkbox and I want use (change) on it. Default is "checked", but after click I want clear input text "Activation key". After re-checked I want generate guid and add again to input. How to get if the checkbox is selected or no?

<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 });
    }
}

Upvotes: 4

Views: 6791

Answers (1)

Pengyy
Pengyy

Reputation: 38171

you can get checkbox status by e.target.checked

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

Upvotes: 5

Related Questions