Within my TypeScript file, I am dynamically generating properties on the object named selectedValsObj
in the following manner:
private selectValsObj: any = {};
setSelectedValsObj(sectionsArr) {
sectionsArr.forEach(section => {
section.questions.forEach(questionObj => {
if (questionObj.type === 'drop-down') {
this.selectValsObj[questionObj.questionId] = { selected: questionObj.answerDetails[0] };
}
})
});
}
When it comes to the HTML part, I desire to bind the [ngModel]
attribute of my inputs to the properties existing within the selectValsObj
object. Despite attempting various approaches, I have not been able to achieve the desired outcome:
<div *ngFor="let question of section.questions">
<div class="drop-down-question" *ngIf="question?.type === 'drop-down'">
<select class="q-select"
[(ngModel)]="selectValsObj[questionId].selected" // <== doesn't work either**
// [(ngModel)]="selectValsObj[{{ questionId }}].selected" // <== also doesn't work**
name="answerForQuestion{{ question?.questionId }}">
<option *ngFor="let answer of question?.answerDetails"
[ngValue]="answer">
{{ answer?.value }}
</option>
</select>
</div>
</div>
Is there a way for me to properly assign the ngModel
within my HTML to a property that is created dynamically within my TypeScript file?