简体   繁体   English

Angular 单选按钮使用 ngFor 显示错误数据

[英]Angular radio button show wrong data with ngFor

I am new on angular, need your assistance in one of my problems.我是 angular 的新手,需要您帮助解决我的一个问题。 I have a scenario where I am getting an array of objects from API like我有一个场景,我从 API 获取对象数组,例如

[
    {class: "X", subject: [{name: "abc", score: 1},{name: "def", score: 3}]},
    {class: "XI", subject: [{name: "abc", score: 2},{name: "def", score: 2}]},
    {class: "XII", subject: [{name: "abc", score: 3},{name: "def", score: 1}]}
]

here score is from 0 to 3. And I have to show the score on UI like below image这里的分数是从 0 到 3。我必须在 UI 上显示分数,如下图所示在此处输入图像描述

a first radio button is for score 0 and second for score 1 till last ie score 3第一个单选按钮用于得分 0,第二个单选按钮用于得分 1 直到最后,即得分 3

and my angular code is我的 angular 代码是

<div *ngFor="let class of classes; let ind = index">
    {{class.class}}
    <div *ngFor="let mark of class.subject; let ind = index">
            {{mark.name}}
            <input type="radio" [value]="0" name="score" id="zero"
                                                        [(ngModel)]="mark.score">
            <input type="radio" [value]="1" name="score" id="one"
                                                        [(ngModel)]="mark.score">
            <input type="radio" [value]="2" name="score" id="two"
                                                        [(ngModel)]="mark.score">
            <input type="radio" [value]="3" name="score" id="three"
                                                        [(ngModel)]="mark.score">
        
    </div>
</div>

If I run the code, I am getting only last name score in list is checked with the score, and the rest of the above scores show blank.如果我运行代码,我只得到列表中的姓氏分数与分数一起检查,并且上述分数的 rest 显示为空白。

PS: And make edit score again with the new scores. PS:并用新的分数再次编辑分数。

Thanks.谢谢。

PLease have a look at this solution and working stackblitz请看看这个解决方案和工作stackblitz

<div *ngFor="let class of classes; let i = index">
  {{class.class}}
  <div *ngFor="let subject of class.subject; let j = index">
        {{subject.name}}
        <input type="radio" [value]="0" name="score_{{i}}_{{j}}" id="zero"
                                                    [(ngModel)]="subject.score">
        <input type="radio" [value]="1" name="score_{{i}}_{{j}}" id="one"
                                                    [(ngModel)]="subject.score">
        <input type="radio" [value]="2" name="score_{{i}}_{{j}}" id="two"
                                                    [(ngModel)]="subject.score">
        <input type="radio" [value]="3" name="score_{{i}}_{{j}}" id="three"
                                               [(ngModel)]="subject.score">
  </div>
</div>

<div >
 <ul *ngFor="let class of classes;">
  <span style="background:cyan"> Class :  {{class.class}}</span>
   <li *ngFor="let subject of class.subject;">
     <span style="background:yellow">  Subject Name : {{subject.name }}</span><br/>
      <span style="background:pink">   Score : {{subject.score }}</span>
     </li>
 </ul>
</div>

Firstly the data should be: (there's a typo in the question)首先数据应该是:(问题中有错字)

classes = [
  {
     class: "X",
     subject: [{ name: "abc", score: 1 }, { name: "def", score: 3 }]
  },
  {
     class: "XI",
     subject: [{ name: "abc", score: 2 }, { name: "def", score: 2 }]
  },
  {
     class: "XII",
     subject: [{ name: "abc", score: 3 }, { name: "def", score: 1 }]
  }
];

Next your template should use the following logic:接下来,您的模板应使用以下逻辑:

<div *ngFor="let class of classes; let i = index">
    {{class.class}}

    <div *ngFor="let mark of class.subject; let j = index">
        {{mark.name}}
       <input type="radio" [value]="0" [(ngModel)]="mark.score" 
              id="{{i}}_{{j}}" name="{{i}}_{{j}}"  >
       <input type="radio" [value]="1" [(ngModel)]="mark.score"
              id="{{i}}_{{j}}" name="{{i}}_{{j}}">
       <input type="radio" [value]="2" [(ngModel)]="mark.score"
              id="{{i}}_{{j}}" name="{{i}}_{{j}}">
       <input type="radio" [value]="3" [(ngModel)]="mark.score"
              id="{{i}}_{{j}}" name="{{i}}_{{j}}">              
    </div>

</div>

The reason your code didn't work was because the id and name attributes being generated on the template were not unique.您的代码不起作用的原因是模板上生成的idname属性不是唯一的。 So when you change the score for one student all the other scores were reflecting the same.因此,当您更改一个学生的分数时,所有其他分数都反映相同。 To resolve this use proper indices from the inner and outer loops to maintain uniqueness for both attributes as shown above.要解决此问题,请使用内部和外部循环中的适当索引来保持两个属性的唯一性,如上所示。

In you code name attribute of radio button is same for all the radio buttons.在您中,单选按钮的代码名称属性对于所有单选按钮都是相同的。 Try this尝试这个

name="score{{i}}_{{j}}"

Also you have taken same index variable for inner and outer for loop.此外,您为内部和外部 for 循环采用了相同的索引变量。 please change that as well.请也改变它。 Find full html below.在下面找到完整的 html。

<div *ngFor="let class of classes; let i = index">
  {{class.class}}
  <div *ngFor="let mark of class.subject; let j = index">
        {{mark.name}}
        <input type="radio" [value]="0" name="score{{i}}_{{j}}" id="zero"
                                                    [(ngModel)]="mark.score">
        <input type="radio" [value]="1" name="score{{i}}_{{j}}" id="one"
                                                    [(ngModel)]="mark.score">
        <input type="radio" [value]="2" name="score{{i}}_{{j}}" id="two"
                                                    [(ngModel)]="mark.score">
        <input type="radio" [value]="3" name="score{{i}}_{{j}}" id="three"
                                                    [(ngModel)]="mark.score">
    
  </div>
</div>

hope this will solve your problem.希望这能解决你的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM