我使用 *ngFor
来填充我的表。
<tr *ngFor= "stu in students">
<td>{{stu.name}}</td>
<td><input type="checkbox" ngModel="isChecked"
(ngModelChange)="addID(stu.id)</td>
</tr>
然后我在 table 外面有一个按钮。
<button (click)="selectAllID()">select all</button>
然后我的组件是:
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
}
问题是:
如果我选中单个复选框,则会执行 addID()
函数。但是如果我点击全选按钮,复选框就会被选中。但是 addID()
函数没有被调用。
当我使用全选按钮时,如何触发 ngModelChange
函数,以便获得所有选定的 id
最佳答案
您只有一种方法可以将输入控件与 ngModelChange
指令绑定(bind)。但是要更新 View ,您应该将控件绑定(bind)到 ngModel
。
<input type="checkbox" [ngModel]="isChecked"
(ngModelChange)="addID(stu.id)">
关于html - 如何使用 Angular 2 中的按钮触发 ngModelChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44935702/