简体   繁体   English

如何在 angular 的 api 中使用无线电组中的 ngModel

[英]How to use ngModel in the radio group from the api in angular

here's the code.这是代码。

list.component.html列表.component.html

<form nz-form [formGroup]="taskFormGroup" (submit)="saveFormData()">
    <div nz-row *ngFor="let remark of checklist">
              <div nz-col nzXXl="12" *ngFor="let task of remark.tasks" style="padding: .5rem;">
                <div nz-row nzGutter="6" nzType="flex" nzAlign="middle" style="border-left: 5px solid rgba(167, 0, 51, 0.5);">
                  <div nz-col nzSpan="8">
                    <label [textContent]="task.name"></label>
                  </div>
                  <div nz-col nzSpan="8">
                    <nz-form-item>
                      <nz-form-control>
                        <nz-radio-group formControlName="radiostatus" [(ngModel)]="radio" (ngModelChange)="onChangeStatus($event)">
                          <label nz-radio nzValue="passed">Passed</label>
                          <label nz-radio nzValue="failed">Failed</label>
                        </nz-radio-group>
                      </nz-form-control>
                    </nz-form-item>
                  </div>
                </div>
              </div>
            </div>
  </form>

list.component.ts列表.component.ts

checklist = [
    {
      "id": "txv3vvBr8KYB",
      "assetType": {
        "id": "1fKBO4w0XHg7H",
        "code": "PRD",
        "name": "Printing1"
      },
      "tasks": [
        {
          "id": "1fKBO4w0XHg7H",
          "name": "Task 1",
          "description": "Check oil spill"
        },
        {
          "id": "ESOSA6aCrOER",
          "name": "Sample1",
          "description": "Desc1"
        }
      ]
    },
    {
      "id": "EwQciw9whx6B",
      "tasks": [
        {
          "id": "1nU7uASqfvLPD",
          "name": "TASK8888",
          "description": "DESC8888"
        },
        {
          "id": "EwQciw9whx6B",
          "name": "TASK9999",
          "description": "DESC9999"
        }
      ]
    }
  ];

When selecting on the passed or failed, when it select on the 1st item it shouldn't affect the 2nd item.在传递或失败中选择时,在第1项上选择时,它不应影响第二项。 for example on the 1st item, it select the passed in the second item it shouldn't select the passed on the second.例如,在第一个项目上,它选择第二个项目中传递的内容,它不应该选择第二个项目中的传递内容。

on my part when I select the passed on the first item, it affect the second item which it select also the passed.就我而言,当我选择第一个项目的传递时,它会影响它选择的第二个项目也传递。

In your code you have same ngModel binding mame for all form control.在您的代码中,您对所有表单控件都有相同的 ngModel 绑定。 Try to assign unique name尝试分配唯一名称

component.html组件.html

<div nz-row *ngFor="let remark of checklist; let  i = index">
      <div nz-col nzXXl="12" *ngFor="let task of remark.tasks" style="padding: .5rem;">
        <div nz-row nzGutter="6" nzType="flex" nzAlign="middle" style="border-left: 5px solid rgba(167, 0, 51, 0.5);">
          <div nz-col nzSpan="8">
            <label [textContent]="task.name"></label>
          </div>
          <div nz-col nzSpan="8">
            <nz-form-item>
              <nz-form-control>
                <nz-radio-group [(ngModel)]="task.id" (ngModelChange)="onChangeStatus($event)">
                  <label nz-radio nzValue="passed">Passed</label>
                  <label nz-radio nzValue="failed">Failed</label>
                </nz-radio-group>
              </nz-form-control>
            </nz-form-item>
          </div>
        </div>
      </div>
 </div>

Example例子

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

相关问题 单选组选择的选项不更新角度中的ngModel值 - Radio group selected option not updating ngModel value in angular Angular 6和Angular Material-mat-radio-group [(ngModel)]在* ngFor内部设置动态变量 - Angular 6 and Angular Material - mat-radio-group [(ngModel)] set dynamic variable inside *ngFor 角度2:如何在Component中使用输入[[ngModel)]值? - Angular 2: How to use input [(ngModel)] value in Component? 如何以角度固定无线电组 - How to fix the radio group in angular 如何在离子无线电元素上使用 ngModel? - How can I use a ngModel on an ion-radio element? 为什么无线电组的[(ngModel)]不会绑定? - Why won't a [(ngModel)] of a radio group bind? 在Angular.js中更新单选按钮ngModel - Updating ngModel of radio buttons in Angular.js angular 6 ngModel 带单选按钮不传递任何数据 - angular 6 ngModel with radio button not passing any data 无法读取未定义的属性&#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 来自 mat-radio 组的 angular 过滤垫收音机显示器 - angular filter mat radio display from a mat-radio group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM