I am working on Angular 5 application with checkbox. I have data coming from database, followed by mapped to javaScript object. I am trying to configure 'Selected' value but unable to do so. In my result, I got all boxes checked which should be.
in following code, option: [] --> 'optionSelected' defined if is selected value by true or false,
produced schema as following;
Template
<div *ngSwitchCase="'checkbox'"> <small>checkbox</small>
<div *ngFor="let opt of question.options" >
<label class="container-vertical"> {{opt.value}}
<input type="checkbox" [formControlName]="question.key" [name]="opt.name" [value]="opt.key" [checked]="opt.optionSelected" />
<span class="checkmark2"></span>
</label>
</div>
</div>
also tried with ngModel as in my component, property 'value' holds id of selected key but still final result checked all boxes
<input type="checkbox" [formControlName]="question.key" [name]="opt.name" [value]="opt.key" [(ngModel)]="question.value" />
component
else if(questionElementType=="checkbox")
{
let _checkBox = new CheckBoxQuestion ({
consultationId: questionsList[key].consultationId,
questionId: questionsList[key].questionId,
questionElementType: questionsList[key].questionElementType[0].title,
questionType: questionsList[key].questionType,
title:questionsList[key].title,
displayId: questionsList[key].displayId,
key: questionsList[key].questionId,
value: questionsList[key].answer.length<=0? null : questionsList[key].answer[0].answerValue.toLowerCase(),
label: questionsList[key].title,
order: 7,
options: questionsList[key].answerOptions.map(function(item){
return {"name": item.ReferenceKey, "key": item.preDefineAnswerOptionId, "value": item.text, "optionSelected": item.preDefineAnswerOptionId==questionsList[key].answer[0].answerValue? "true": "false"}
}),
});
this.mappedQuestions.push(_checkBox);
}

