mercredi 5 juillet 2017

How to trigger ngModelChange with a button in Angular2

I use *ngFor to populate my table.

<tr *ngFor= "stu in students">
<td></td>
<td><input type="checkbox" ngModel="isChecked" 
(ngModelChange)="addID(stu.id)</td>
</tr>

Then I have a button outside the table.

<button (click)="selectAllID()">select all</button>

Then I have my component as:

studentID=[];
isChecked=false;

AddID(id:number){
this.student.push (id);
//I do other thing with id
}

selectAllID (){
 If (this.isChecked)
     this isChecked = false;
  else this isChecked = true
}

The problem is. if I check individual checkbox, the AddID function is executed. But if I click on the select all button, the checkboxes get selected. But the addID function is not called.

How can I trigger the ngModelChange function when I use the select all button so I get all selected id's




Aucun commentaire:

Enregistrer un commentaire