简体   繁体   English

Angular ngModel 未在 ngFor 中设置单选按钮值

[英]Angular ngModel not set radio button value inside ngFor

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

[{name: "abc", score: 2},{name: ""def, score: 3}]

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

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 mark of marks; 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>

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

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

Thanks.谢谢。

Having different name for each radio button will solve the problem.为每个单选按钮设置不同的名称将解决问题。

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

This is because ngModel for each radio will be the one which is selected.这是因为每个收音机的 ngModel 都是被选中的。 Try to bind new variable to ngModel.. Try this: https://stackblitz.com/edit/angular-radio-button-api尝试将新变量绑定到 ngModel.. 试试这个: https://stackblitz.com/edit/angular-radio-button-api

Good that you accepted answer;很好,您接受了答案; but to answer the question in title.但要回答标题中的问题。

Set value to index , like:value设置为index ,例如:

<div *ngFor="let item of itemList; let i = index">

  <input type="radio" name="picker"
         [value]="i"
         [(ngModel)]="item.isPicked">

</div>

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

相关问题 Angular 6和Angular Material-mat-radio-group [(ngModel)]在* ngFor内部设置动态变量 - Angular 6 and Angular Material - mat-radio-group [(ngModel)] set dynamic variable inside *ngFor * ngFor内的[[ngModel)]角 - angular [(ngModel)] inside *ngFor 无法读取未定义的属性&#39;0&#39;-Angular 6和Angular Material-mat-radio-group [(ngModel)]在* ngFor内部设置动态变量 - Cannot read property '0' of undefined - Angular 6 and Angular Material - mat-radio-group [(ngModel)] set dynamic variable inside *ngFor 角度6中的自定义单选按钮选择错误(在ngFor内部) - Custom Radio button selection bug in angular 6 (inside of ngFor) 如何在 ngFor 中单击 angular 以突出显示选定的单选按钮 - How to highlight the selected radio button with on click in angular inside ngFor Angular 4-* ngFor,条件为ngModel获得不同的值 - Angular 4 - *ngFor with condition to get different value for ngModel Angular 2:ngfor与ngmodel的取值错误 - Angular 2: ngfor with ngmodel gets wrong value angular 6 ngModel 带单选按钮不传递任何数据 - angular 6 ngModel with radio button not passing any data Angular 单选按钮使用 ngFor 显示错误数据 - Angular radio button show wrong data with ngFor 使用角度模型单击按钮后,将值设置为单选按钮 - Set value to radio button after click on button using angular model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM