在我的 TS 文件中,我动态地在我的selectedValsObj
对象上创建属性,如下所示:
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] };
}
})
});
}
在我的 HTML 中,我想将[ngModel]
我的输入绑定到selectValsObj
对象的属性。我试过这个但没有运气:
<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" // <== doesnt work either**
// [(ngModel)]="selectValsObj[{{ questionId }}].selected" // <== doesnt work**
name="answerForQuestion{{ question?.questionId }}">
<option *ngFor="let answer of question?.answerDetails"
[ngValue]="answer">
{{ answer?.value }}
</option>
</select>
</div>
</div>
如何ngModel
在我的 HTML 中将 TS 文件中的动态创建属性设置为?