code1
code1

Reputation: 9029

How to call a function on checking a checkbox in angular 2.0

I am currently using angular 2.0. In HTML, I have a checkbox, the code is as mentioned below.

<div style="margin-left:15px;">
            <label>Draw</label>
            <input id="isCheckBox" type="checkbox" checked="checked">
</div>

How to call a function only if a checkbox is checked in typescript file?

Upvotes: 10

Views: 33154

Answers (2)

Pradeep Kumar Das
Pradeep Kumar Das

Reputation: 891

You can use currentTarget if target won't work,

<input type="checkbox"  id="isCheckBox" (change)="yourfunc($event)"/>

and in typescript,

yourfunc(e) {
   if(e.currentTarget.checked){        
   }
}

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222722

You can do this,

<input type="checkbox"  id="isCheckBox" (change)="yourfunc($event)"/>

and in typescript,

yourfunc(e) {
   if(e.target.checked){        
   }
}

Upvotes: 31

Related Questions