Angular2,Typescript:如何在一个每页显示一个元素的数组中检查单选按钮?

前端之家收集整理的这篇文章主要介绍了Angular2,Typescript:如何在一个每页显示一个元素的数组中检查单选按钮?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
让我详细解释一下.对问题框架进行解释.

我有一个存储在REST API中的对象数组.该数组包含问题和选择.每个问题有4个选项,即单选按钮.

我试图在同一页面上一次显示一个问题以及问题的选择.我有两个按钮“上一个”和“前进”,它将问题加载到同一页面上.

当我单击下一个按钮时,将显示阵列中的下一个问题以及选项.当我单击上一个按钮时,显示上一个问题,但不显示“使用选中的单选按钮”.
现在我的要求是,即使在进入下一个问题之后,上一个问题中检查的单选按钮也必须保持检查,当我点击之前它必须向我显示我检查了哪个单选按钮.

我正在尝试使用单选按钮提供的[已选中]选项.但我无法得到它.这是我的代码

  1. <div *ngIf="questions[indexVal]">
  2. {{indexVal+1}} {{questions[indexVal].Text}}
  3. <div *ngFor="let choiceObj of questions[indexVal].choices">
  4. <input name='question' type='radio' [value]='choiceObj' [(ngModel)]='selectedchoice' (ngModelChange) = "storeChoice(choiceObj.choiceId,questions[indexVal])"
  5. [checked]="check(questions[indexVal])"/>
  6.  
  7. <label [for]='choiceObj'> {{choiceObj.choiceText}} </label>
  8.  
  9. </span>
  10. </div>
  11. </div>
  12. <button ion-button value="prevIoUs" (click)="PrevQues()" [disabled] =
  13. "disableprev"> PREVIoUS </button>
  14. <button ion-button value="next" (click)="NextQues()"> NEXT </button>

最初indexVal = 0,这意味着它指向第一个问题.点击下一个按钮,它变为indexVal = 1,而之前它变为indexVal- = 1;

这是我的打字稿代码.我正在尝试维护一个名为“answers”的数组,它存储所选择的选项.

  1. storeChoice(choiceId,questions[indexVal])
  2. {
  3. questions[indexVal].answers[0] = choiceId;
  4.  
  5. }
  6.  
  7. check(questions[indexVal])
  8. {
  9. return questions[indexVal].answers[0];
  10. }
您可以将答案绑定到问题[index] .answer,而不是将selectedChoice属性用于所有检查.
请看一下 this working plunker

这是结果:

正如你在那个plunker中看到的那样,我正在约束问题的答案.直接:

  1. <ion-list radio-group [(ngModel)]="question.answer">
  2. <ion-item no-lines no-padding *ngFor="let choice of question.choices">
  3. <ion-label>{{ choice.choiceText }}</ion-label>
  4. <ion-radio [value]="choice"></ion-radio>
  5. </ion-item>
  6. </ion-list>

另请注意,我在更改问题时使用滑块添加效果很好.

查看代码

  1. <ion-content padding>
  2.  
  3. <ion-slides>
  4. <ion-slide *ngFor="let question of questions; let i = index">
  5. <h1>{{ question.text }}</h1>
  6. <ion-list radio-group [(ngModel)]="question.answer">
  7. <ion-item no-lines no-padding *ngFor="let choice of question.choices">
  8. <ion-label>{{ choice.choiceText }}</ion-label>
  9. <ion-radio [value]="choice"></ion-radio>
  10. </ion-item>
  11. </ion-list>
  12.  
  13. <span style="font-size: 1.2rem;">Selected answer: {{ question.answer | json }}</span>
  14.  
  15. <ion-row margin-top>
  16. <ion-col>
  17. <button (click)="showPrevIoUs()" ion-button text-only [disabled]="i === 0" >PrevIoUs</button>
  18. </ion-col>
  19. <ion-col>
  20. <button [disabled]="!question.answer" *ngIf="i < questions.length - 1" (click)="showNext()" ion-button text-only >Next</button>
  21. <button [disabled]="!question.answer" *ngIf="i === questions.length - 1" (click)="showNext()" ion-button text-only >Submit</button>
  22. </ion-col>
  23. </ion-row>
  24.  
  25. </ion-slide>
  26. </ion-slides>
  27.  
  28. </ion-content>

组件代码

  1. import { Component,ViewChild } from '@angular/core';
  2. import { NavController,Content,Slides } from 'ionic-angular';
  3.  
  4. @Component({...})
  5. export class HomePage {
  6. @ViewChild(Slides) slides: Slides;
  7.  
  8. public questions: Array<{ text: string,answer: number,choices: Array<{choiceId: number,choiceText: string}> }>
  9.  
  10. ngOnInit() {
  11. this.slides.lockSwipes(true);
  12. }
  13.  
  14. constructor() {
  15.  
  16. this.questions = [
  17. {
  18. text: 'Question 1',choices: [
  19. {
  20. choiceId: 1,choiceText: 'Choice 1-1'
  21. },{
  22. choiceId: 2,choiceText: 'Choice 1-2'
  23. },{
  24. choiceId: 3,choiceText: 'Choice 1-3'
  25. },{
  26. choiceId: 4,choiceText: 'Choice 1-4'
  27. }
  28. ],answer: null
  29. },{
  30. text: 'Question 2',choices: [
  31. {
  32. choiceId: 21,choiceText: 'Choice 2-1'
  33. },{
  34. choiceId: 22,choiceText: 'Choice 2-2'
  35. },{
  36. choiceId: 23,choiceText: 'Choice 2-3'
  37. },{
  38. choiceId: 24,choiceText: 'Choice 2-4'
  39. },],{
  40. text: 'Question 3',choices: [
  41. {
  42. choiceId: 31,choiceText: 'Choice 3-1'
  43. },{
  44. choiceId: 32,choiceText: 'Choice 3-2'
  45. },{
  46. choiceId: 33,choiceText: 'Choice 3-3'
  47. },{
  48. choiceId: 34,choiceText: 'Choice 3-4'
  49. },answer: null
  50. }
  51. ]
  52.  
  53. }
  54.  
  55. public showPrevIoUs(): void {
  56. this.slides.lockSwipes(false);
  57. this.slides.slidePrev(500);
  58. this.slides.lockSwipes(true);
  59. }
  60.  
  61. public showNext(): void {
  62. this.slides.lockSwipes(false);
  63. this.slides.slideNext(500);
  64. this.slides.lockSwipes(true);
  65. }
  66.  
  67. }

猜你在找的Angularjs相关文章