单选按钮绑定:Angular 5

时间:2018-04-21 20:07:58

标签: html angular angularjs-directive

这段代码主要用于带有4个选项的MCQ。我每次用户选择并使用单选按钮提交正确的答案时都会尝试增加计数器(var)。但单选按钮绑定不起作用不知何故。 在stackoverflow上尝试使用Google搜索,但提供的解决方案还不够。

HTML:

div *ngFor="let abc of questArrayNew1 ;let j = index" >

           {{j+1}}.{{abc.OP[j]}}

          <br>
        <input type="radio" name="s" [(ngModel)]="hello" [value]="abc.OP[j+1]" >{{abc.OP[j+1]}}

        <br>  
        <input type="radio" name="s" [(ngModel)]="hello" [value]="abc.OP[j+2 ]">{{abc.OP[j+2]}}

        <br>
        <input type="radio" name="s" [(ngModel)]="hello" [value]="abc.OP[j+3]">{{abc.OP[j+3]}}

        <br>
      <input type="radio" name="s" [(ngModel)]="hello" [value]="abc.OP[j+4]">{{abc.OP[j+4]}}

        <br>

          </div>'
<div>
    <button type="submit" class="btn btn-light" id="button1" (click)="submitAns(radio)">Submit</button>
</div>

.TS:

 submitAns(hello:string)
  {

  {
     if (hello == this.questArrayNew1[1]) //Checking if ans is right

    {
      console.log(hello); //undefined is being returned
      console.log(this.questArrayNew1); //this is returning perfect array
      this.counter++;

    }
    else
    { 

      console.log("Wrong answer");
    };

  } 

}

JSON:

[  
  {


    "OP":["The Government of India (GoI) has launched the web-based application e-FRRO scheme for foreigners. What does ‘FRRO’ stands for ?",
    "Foreigners Regional Registration Office" , 
    "Foreigners Registry Registration Office", 
    "Foreigners Reacceptance Registration Office", 
    "Foreigners Regaining Registration Office" ]

},

  {


        "OP":["QUES2","ADASD" , "ASDAD", "ASDASD", "ASDADS"]

    }


]

1 个答案:

答案 0 :(得分:-1)

这已修复:

<div>
    <button type="submit" class="btn btn-light" id="button1" (click)="submitAns(hello)">Submit</button>
</div>