【发布时间】:2020-02-22 16:23:45
【问题描述】:
我正在设计一个反馈页面,其中有多个问题被问到,并且所有问题都有相同的多项选择答案。所以我设计如下。
options: string[] = ['Excellent', 'Fair', 'Good', 'Poor'];
questions: any = [
{
question: "How would you rate A",
answer: this.options,
radioSelected: ""
},
{
question: "How would you rate B",
answer: this.options,
radioSelected: ""
},
{
question: "How would you rate C",
answer: this.options,
radioSelected: ""
},
{
question: "How would you rate D",
answer: this.options,
radioSelected: ""
},
{
question: "How would you rate E",
answer: this.options,
radioSelected: ""
}
];
<div *ngFor="let q of questions; index as i">
<label id="example-radio-group-label">{{q.question}}</label>
<div class="row">
<div class="form-check" *ngFor="let option of q.answer">
<div class="col-md-3">
<input class="form-check-input" id="{{option}}" [value]="option" type="radio" name="options"
[(ngModel)]="q.radioSelected">
<label class="form-check-label" for="{{option}}">
{{option}}
</label>
</div>
</div>
</div>
<hr>
</div>
代码在 UI 中显示良好。但是当我单击任何问题的单选按钮时,只会选择第一个问题的选项。我是 Angular 新手,请帮我解决这个问题。
【问题讨论】:
-
您可以使用第二个索引,例如:
*ngFor="let option of q.answer;let j = index"
标签: html angular typescript