Javascript 如何检查我的复选框列表是否选中AngularJS 2
我有一个复选框列表Javascript 如何检查我的复选框列表是否选中AngularJS 2,javascript,angular,typescript,checkbox,Javascript,Angular,Typescript,Checkbox,我有一个复选框列表 documentType: IDocument[] = [ { "type": "A", "checked": false, }, { "type":"B", "checked": false, }, { "type":"C" , "checked": false, }]; 我在模板上的复选框列表中显示此数组: <ion-list *ngFor
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>
但这不是合适的方式。我不知道哪个元素被选中或取消选中。
你能帮我找出使用多个复选框的最佳实践吗。因为我想用服务设置数组。我要我的密码
可重复使用的。因此,我将只更改web api。
提前感谢您因为您正在使用
[(ngModel)]=“type”将复选框绑定到对象。选中“
,您已经拥有了所需的所有数据。您只需在阵列上循环以检查选中的复选框
checkBoxChecked()
{
this.documentType.forEach(e => {
if(e.checked){
console.log(e.type+' is checked !")
}
});
}
{{col.name}}
数据:任意[]=[{“id”:“13”,“名称”:“AAA”},{“id”:“15”,“名称”:“BBB”},{“id”:“20”,“名称”:“CCC”}]
构造函数(){}
获取所选复选框(){
返回此.data
.filter(opt=>opt.value)
}
添加列(列){
此选项。选中复选框;
console.log(此.selected复选框)
}
请参阅下面使用属性绑定模型的演示:
谢谢您的回复。它将出现在每个if()语句中。想象一下,我在这个数组中有50个元素!你说的每个if声明是什么意思?我的错。我没有戴眼镜。谢谢你的回复。它很有魅力
checkBoxChecked()
{
this.documentType.forEach(e => {
if(e.checked){
console.log(e.type+' is checked !")
}
});
}
<li *ngFor="let col of data" class="form-group">
<input type="checkbox" name="col" value=" {{col.value}}" [(ngModel)]="col.value" (change)="addColumns(col)" />{{col.name}}
</li>
data:any[]=[{"id":"13","name":"AAA"},{"id":"15","name":"BBB"}, {"id":"20","name":"CCC"}]
constructor() {}
get selectedcheckboxes() {
return this.data
.filter(opt => opt.value)
}
addColumns(col){
this.selectedcheckboxes;
console.log(this.selectedcheckboxes)
}
See The demo below using the property binding-ngModel :