简体   繁体   English

我正在使用 angular 和 spring boot 开发考试门户,当我使用 name 作为 [name] 时出现问题

[英]I am working on exam portal using angular and spring boot, I got a problem here when i am using name as [name]

Component.html组件.html

   <div class="bootstrap-wrapper" *ngIf="!isSubmit">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-2">
                <!-- instructions -->
                <h2>Instructions</h2>
            </div>
            <div class="col-md-8">
                <!-- questions -->
                <ng-container *ngIf="questions">
                    <h2>{{questions[0].quiz.title}}</h2>

                </ng-container>
                <mat-card *ngFor="let q of questions, let i= index" class="mt20">

                    <mat-card-content>
                        <p> Q {{i+1}}) <span [innerHTML]="q.content"></span> </p>
                      
                        <mat-divider></mat-divider>
                        <div class="row mt20" >
                            <div class="col-md-6">
                                <input  type="radio" [value]="q.option1" 
                                    [name]="i"
                                       // this is where i am getting error
                                [(ngModel)] ="q.givenAnswer"
                                />
                                {{q.option1}}
                                {{i}}

                            </div>
                            <div class="col-md-6">
                                <input  type="radio" [value]="q.option2" 
                                 [name]="i"
                                  // this is where i am getting error
                                [(ngModel)] ="q.givenAnswer"
                               />
                                {{q.option2}}
                                {{i}}
                            </div>
                        </div>
                        <div class="row mt20">
                            <div class="col-md-6">
                                <input  type="radio" [value]="q.option3" 
                                   // this is where i am getting error
                                 [name]="i"
                                [(ngModel)] ="q.givenAnswer"
                               />
                                {{q.option3}}
                                {{i}}
                            </div>
                            <div class="col-md-6">
                                <input  
                                type="radio" 
                                [value]="q.option4" 
                                 // this is where i am getting error
                                [name]="i"
                                [(ngModel)] ="q.givenAnswer"
                                />
                                {{q.option4}}
                                {{i}}
                            </div>
                        </div>
                    </mat-card-content>

                </mat-card>
                <div class="container text-center mt20">
                    <button (click)="submitQuiz()" mat-raised-button color="accent">Submit 
       Quiz</button>
                </div>

            </div>
            <div class="col-md-2">

            </div>
        </div>
    </div>
    </div>

    <!-- Show Result -->
    <div class="bootstrap-wrapper" *ngIf="isSubmit">
    <div class="row">
        <div class="col-md-6 offset-md-3">
            <mat-card>
                <mat-card-header>
                    <mat-card-title>
                        <h1 class="text-center mall">Quiz Result</h1>
                    </mat-card-title>
                </mat-card-header>
                <mat-card-content>
                    <h1>Marks Obtained: {{marksGot}}</h1>
                    <h1>Correct Ansers: {{correctAnswers}}</h1>
                    <h1>Questions Attempted: {{attempted}}</h1>
                </mat-card-content>
                <mat-card-actions>
                    <button mat-raised-button color="accent">Print</button>
                    <button mat-raised-button color="accent" [routerLink]="'/user- 
       dashboard/0'">Home</button>
                </mat-card-actions>
            </mat-card>
        </div>
    </div>
    </div>

Component.ts组件.ts



    import { LocationStrategy } from '@angular/common';
    import { Component, OnInit } from '@angular/core';
    import { ActivatedRoute } from '@angular/router';
    import { QuestionService } from 'src/app/services/question.service';
    import Swal from 'sweetalert2';

    @Component({
    selector: 'app-start-quiz',
    templateUrl: './start-quiz.component.html',
     styleUrls: ['./start-quiz.component.css']
    })
    export class StartQuizComponent implements OnInit {

    qid;
    questions;
    marksGot = 0;
    correctAnswers = 0; 
    attempted = 0;

    isSubmit = false;

    constructor(private locationSt: LocationStrategy, private _route: ActivatedRoute, private 
    _question: QuestionService) { }

    ngOnInit(): void {

    this.preventBackButton();
    this.qid = this._route.snapshot.params['qid'];
    this.loadQuestions();
    }

    loadQuestions() {
    this._question.getQuestionsOfQuizForTest(this.qid).subscribe(
      (data: any) => {
        this.questions = data;

        this.questions.forEach((q) => {
          q['givenAnswer'] = '';
        });
        console.log(data);

      },
      (error) => {
        Swal.fire('Error', 'Error in loading questions of quiz', 'error');
      }
       );
    }

    preventBackButton() {
    history.pushState(null, null, location.href);
    this.locationSt.onPopState(() => {
      history.pushState(null, null, location.href);
    })
    }

    submitQuiz() {
    Swal.fire({
      title: 'Do you want to Submit quiz?',
      showCancelButton: true,
      confirmButtonText: 'Submit Quiz',
      icon: 'info',
    }).then((e) => {
      if (e.isConfirmed) {
        //calculation
        this.isSubmit=true;
        this.questions.forEach((q) => {
          if (q.givenAnswer == q.answer) {
            this.correctAnswers++;
            let marksSingle = this.questions[0].quiz.maxMarks / this.questions.length;
            this.marksGot += marksSingle;
          }

          if (q.givenAnswer.trim() != '') {
            this.attempted++;
          }
        });
        console.log("Correct Answers " + this.correctAnswers);
      }
    })
     }
   }

enter image description here在此处输入图像描述

When i am name as [name] it is showing number is not assignable to type String and when i am using name it is compiling successfully but i have three questions in a quiz and while selecting an option of a particular question other options of other questions are getting deselected.当我将名称命名为 [name] 时,它显示数字不可分配给类型 String,当我使用名称时,它正在成功编译,但我在测验中有三个问题,并且在选择特定问题的选项时,其他问题的其他选项正在被取消选中。 what to do?该怎么办?
Thanks in Advance提前致谢

[(ngModel)]="q.givenAnswer" type="radio" [value]="q.option1" name={{i}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我使用的是角度2,当我开始输入2个字符时,我想从下拉列表中获取餐厅名称 - I am using angular 2 and i want to fetch restaurant name from drop down when i start entering 2 characters 我对使用三元有点困惑 - I am kinda confused here on using ternary 我在这里正确使用模式匹配吗? - Am I using pattern matching correctly here? 我使用了与验证电子邮件名称相同的逻辑,但是由于某种原因,它无法正常工作 - I am using the same logic that I used to validate the name to the email but for some reason it's not working 我无法使用变量名访问对象函数? - I am not able to access object function using variable name? 我正在使用angular 2,并且已经根据需要创建了一个表单和Marked字段,但是仍然提交了 - I am using angular 2 and I have created a form and Marked fields as required but still my got submitted 使用angular指令绘制高图饼图但是当我在成功函数中使用它时它不起作用 - using angular directive to draw highchart pie chart but when i am using it in success function it is not working setTimeout不起作用,我在这里错过了什么吗? - setTimeout not working,am I missing something here? 使用解析的字符串时,为什么会出现错误“无法读取null的属性&#39;名称&#39;”? - Why am I getting error 'Cannot read property 'name' of null' when using a parsed string? 我在使用复选框时遇到问题 - I am getting a problem while using checkboxes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM