单选按钮绑定:Angular 5
Radio Button binding : Angular 5
此代码基本上用于 MCQ,其中 4 options.I 试图在每次用户使用单选按钮选择并提交正确答案时增加计数器 (var) button.But 单选按钮绑定不是'以某种方式无法正常工作。
尝试在 Whosebug 上进行谷歌搜索,但提供的解决方案还不够。
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"]
}
]
已修复:
<div>
<button type="submit" class="btn btn-light" id="button1" (click)="submitAns(hello)">Submit</button>
</div>
此代码基本上用于 MCQ,其中 4 options.I 试图在每次用户使用单选按钮选择并提交正确答案时增加计数器 (var) button.But 单选按钮绑定不是'以某种方式无法正常工作。 尝试在 Whosebug 上进行谷歌搜索,但提供的解决方案还不够。
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"]
}
]
已修复:
<div>
<button type="submit" class="btn btn-light" id="button1" (click)="submitAns(hello)">Submit</button>
</div>