html - 在 Quiz Angular 5 中处理多个单选按钮

标签 html angular radio-button angular5 angular4-forms

我是 Angular 的新手,正在实现一个包含多个 MCQ 的测验。 但是我在选择单选按钮时遇到了问题。

我的问题也来自数据库和选项。

mcq.component.html

<form (ngSubmit)="ff.form.valid && answer(ff)" #ff="ngForm">
  <div *ngFor="let question of questions">
    <p style="font-size: 25px;">{{question.title}}</p>
    <div *ngFor="let option of question.options">
        <input [(ngModel)]="option_model.selected_option_id" #selected_option_id="ngModel" type="radio" value="{{option.id}}" name="{{question.id}}">
        <!-- <input type="radio" value="{{option.id}}" name="{{question.id}}" ngModel > --> //This way it works fine but I need to use [(ngModel)] to submit the form
      {{option.title}}
    </div>
  </div>
  <input style="float: right" type="submit" value="Submit"/>
</form>

注意:每个问题的 {{question.id}} 都是唯一的。此外,如果我删除 [(ngModel)] 属性,这会很好用。

这就是我想要完成的 And here is what I'm trying to accomplish

问题:当我从第二个问题中选择一个选项时,它取消了从第一个问题中选择的选项。意味着我只能从两个问题中选择一个选项。

请帮助我,我做错了什么。我已经被困在这里两天了。

最佳答案

好的,Git 已排序。问题出在 ngModel 和名称属性上

这样很好用

<input [(ngModel)]="options[question.id]" [checked]="options[question.id]" value="{{question.id}}-{{option.id}}" type="radio"
      name="option{{question.id}}">

在 typescript 中

options: any = [];
option: any = [];

关于html - 在 Quiz Angular 5 中处理多个单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50772057/

相关文章:

Angular 4 : Route Transition Loading

javascript - 选择某个单选按钮时需要在表单上进行某些输入

javascript - ComputeDistanceBetween 返回 NaN 而不是距离

html - 在 nav 元素内垂直对齐 div

HTML/CSS 在段落和无序列表旁边 float 一个 img

Angular 2单元测试: There is no directive with "exportAs"

angular - ngOnChanges Angular 2 不分配不同的值

javascript - 输入属性通过 onclick 事件不可见

php - 使用单选按钮更新记录

html - 如何找出放入 CSS 的正确目标是什么