本文介绍了处理Quiz Angular 5中的多个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular和实现包含多个MCQ的测验的新手.但是我在选择单选按钮时遇到了麻烦.

I'm new to Angular and Implementing a Quiz containing multiple MCQs.But I am having trouble in radio button selection.

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

My Questions are coming from the database and Options too.

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)]属性,效果很好.

Note: The {{question.id}} is unique for each question. Also, this works well if I remove the [(ngModel)] attribute.

这就是我要完成的任务

And here is what I'm trying to accomplish

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

The Problem: When I select an option from the second question it deselects the selected option from the First Question. Means I am only able to select one option from both questions.

请帮助我,我做错了.我已经在这里停留了两天.

Please Help me, what I am doing wrong. I've been stuck here for two days.

推荐答案

好的,把它排序.问题出在ngModel和name属性

Okay, Git it Sorted. The issue was with the ngModel and name attribute

像这样很好用

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

在打字稿中

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

这篇关于处理Quiz Angular 5中的多个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:05