Verify if an input checkbox is checked in Angular2
HTML
<table class="table table-hover" id="just_a_table">
<thead>
<tr>
<th scope="col">Select</th>
<th scope="col">IP Addr</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of instances">
<td><input type="checkbox" (change)='onSelect($event)'></td>
<td>{{data["IP"]}}</td>
</tr>
</tbody>
</table>
You need to check event.target.checked
to solve this issue. This is how you can achieve that:
TS
onSelect(event) {
if ( event.target.checked ) {
// Your logic here
}
}
- You should be using (change) instead of (click) because it's better practice
- Stop thinking on JS. You are now using Typescript and Angular. These frameworks exist because vanilla JS sucks so no need to keep writing vanilla js when you are using this awesome libraries/frameworks