I am currently creating an app using Ionic. I need two checkboxes to see if they are checked or not, so that I can decide where to push the information later.
So I read in the docs that the checkbox framework has an attribute of 'checked' which returns a boolean. First time I tried it, the code looked something like this:
cc.html
<ion-item>
<ion-label>Course Start Date</ion-label>
<ion-checkbox [(ngModel)]="csd"></ion-checkbox>
</ion-item>
<ion-item>
<ion-label>Course End Date</ion-label>
<ion-checkbox [(ngModel)]="ced"></ion-checkbox>
</ion-item>
<button ion-button full (click)="AddNewCourse()">
Add
</button>
cc.ts
csd:any;
ced:any;
AddNewCourse(){
if(this.csd.checked && this.ced.checked){
console.log('pushed here');
}else if(!this.csd.checked && this.ced.checked){
console.log('push there');
}
}
But I eventually got the error cannot read property 'checked' of undefined. So I thought maybe if I initialize the values that would work, so in the constructor:
this.csd = false;
this.ced = false;
Still gave me the same error. So then after searching for a bit I found out that maybe I should use [checked]. But turns out [(ngModel)] and [checked] do not work together which lead me back to zero.
Finally I thought maybe ngOnInit() would do the trick. But unfortunately the same problem persisted.
I'm not sure what I'm doing wrong at this point, my eyes are getting fuzzy so I'm sure I'm doing something stupid and making something as easy so complex. Any help would be appreciated thank you.
Aucun commentaire:
Enregistrer un commentaire