Can't get checkbox values to a <bool> array

Hello, I’m trying to get the correct ngModel syntax for a dynamically set list of checkboxes, which take info from a custom class (MultipleChoice). However, I have been stuck with this error: TypeError: Cannot read property '0' of undefined.

questionClass.ts

export class MultipleChoice {
	questionId: number;
	questionText: string;
	itemList: Array<string>;
	isChecked: Array<boolean>;

	constructor(
		questionId: number,
		questionText: string,
		itemList: Array<string>,
		isChecked?: Array<boolean>

	) {
		this.questionId = questionId; 
		this.questionText = questionText;
		this.itemList = itemList;
	}
}

test.ts

    import {Platform, NavController} from 'ionic-angular';
    import {Component} from '@angular/core';
    import {MultipleChoice} from './../../classes/questionClass/questionClass';

    @Component({
      templateUrl: 'build/pages/test/test.html'
    })

    export class MultipleDirective {
      public question = new MultipleChoice(0,'Expected working hours?',['All hours','9:00 to 17:00','All the weekdays', 'During weekend']);
      constructor(private navController: NavController) { }

  public nextQuestion() {
// TypeError: Cannot read property '0' of undefined 
    this.question.isChecked.forEach((check) => {
      console.log(check);
    });
  }
    }

test.html

<ion-navbar *navbar>
  <ion-title>Test</ion-title>
</ion-navbar>

<ion-content padding class="test">
<ion-list>
  <ion-title>
    {{question .questionText}}
  </ion-title>
  <ion-item *ngFor="let item of question.itemList; let i = index">
    <ion-label>{{item}}</ion-label>
    <ion-checkbox (ngModel)="question.isChecked[i]" ></ion-checkbox>
  </ion-item>
</ion-list>

<button block (click)="nextQuestion()">Next</button>

</ion-content>
1 Like