我有一个复选框列表。
documentType: IDocument[] = [
{
"type": "A",
"checked": false,
},
{
"type":"B",
"checked": false,
},
{
"type":"C" ,
"checked": false,
}];
我在模板的复选框列表中显示这个数组:
<ion-list *ngFor="let type of documentType">
<ion-item >
<ion-label>{{type.documentTypeName}}</ion-label>
<ion-checkbox [(ngModel)]="type.checked" (click)="checkBoxChecked(type.documentTypeName)" disabled="false" ></ion-checkbox>
</ion-item>
</ion-list>
回到我创建 checkBoxChecked 方法的组件:
checkBoxChecked(documentTypeinput)
{
if (documentTypeinput =="A")
{
console.log("this A");
}
else if (documentTypeinput=="B"){
console.log("B")
}
else if (documentTypeinput=="C"){
console.log("C")
}
}
但这不是合适的方式。我不知道哪些元素被选中或未选中。 你能帮我找出使用多个复选框的最佳实践吗?因为我想用服务设置数组。我想要我的代码 可重复使用的。所以我将只更改 web api。 提前谢谢你
最佳答案
由于您使用 [(ngModel)]="type.checked"
将复选框绑定(bind)到您的对象,因此您已经拥有了所需的所有数据。您只需在数组上循环以检查选中了哪个复选框。
checkBoxChecked()
{
this.documentType.forEach(e => {
if(e.checked){
console.log(e.type+' is checked !")
}
});
}
关于javascript - 如何检查我的复选框列表是否已选中或未选中 AngularJS 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42995956/