离子2复选框值
问题描述:
我指的是Ionic 2 read checkbox value代码,与答案中所述的相同。但我在控制台收到错误消息:离子2复选框值
Cannot read property 'indexOf' of undefined
下面是我的代码home.html的:
<form #leadsForm = "ngForm">
<ion-item>
<ion-label>Assign to: </ion-label>
<ion-select [(ngModel)]="saleRep" name="saleRep">
<ion-option *ngFor="let agency of agencyReps;let i = index;" value="{{agencyRepsIds[i]}}"> {{ agencyReps[i] }}</ion-option>
</ion-select>
</ion-item>
<button ion-button (click)="assignLeads()" block>Assign</button>
<ion-list >
<ion-item *ngFor="let t of leads;">
<h2 [ngClass]="t.status">{{t.fname}} {{t.lname}} | {{ t.status}}</span></h2>
<label>
<input type="checkbox" value="{{t}}" [checked]="cbChecked.indexOf('t') >= 0" (change)="updateCheckedOptions(t, $event)" />
</label>
</ion-item>
</ion-list>
</form>
引线在* ngFor直接从后台获取和引线的数据列出完美的罚款。
home.ts如下:
export class Leads {
cbChecked: string[];
saleRep;
constructor() { }
updateCheckedOptions(chBox, event) {
var cbIdx = this.cbChecked.indexOf(chBox);
if(event.target.checked) {
if(cbIdx < 0){
this.cbChecked.push(chBox);
console.log(chBox);
}
} else {
if(cbIdx >= 0){
this.cbChecked.splice(cbIdx,1);
console.log(cbIdx);
}
}
}
assignLeads(){
console.log('Selected Representative',this.saleRep)
console.log('CheckBox Value',this.cbChecked);
}
}
谢谢。
答
两点:
-
cbChecked: string[] = [];
需要有一个初步的价值!否则它的undefined! -
[checked]="cbChecked.indexOf(t) >= 0"
使用t
这里,而不是't'
答
您尚未初始化任何地方的cbChecked
。
集:
constructor() {
this.cbChecked=[];
}
+0
谢谢@suraj的回复。 –
+0
没问题:) ..错过了't'虽然 –
谢谢@mxii。这个对我有用。你挽救了我的另一半天。 –
不客气!祝你好运,快乐编码.. :) – mxii