【问题标题】:How to use ngModel in the radio group from the api in angular如何在角度的api中使用无线电组中的ngModel
【发布时间】:2019-12-14 13:42:49
【问题描述】:

这是代码。

list.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

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"
        }
      ]
    }
  ];

选择传递或失败时,当它在第一项上选择时,它不应影响第二项。例如在第一个项目上,它选择第二个传递的项目,它不应该选择第二个传递的项目。

就我而言,当我在第一个项目上选择传递时,它会影响它也选择传递的第二个项目。

【问题讨论】:

  • 使用 formControl 或 [(ngModel)]。将响应式表单与模板驱动表单一起使用是不好的做法。

标签: javascript angular typescript


【解决方案1】:

在您的代码中,您对所有表单控件都有相同的 ngModel 绑定主机。尝试分配唯一的名称

component.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

【讨论】:

  • RROR TypeError: Cannot assign to read only property 'id' of object '[object Object]' formControlName 是必需的
  • 我更新了代码,这样你就可以看到formGroup
  • 你不应该将模板驱动和响应式表单混合在一起
  • 为什么不应该混合 formGroup 和 ngModel
猜你喜欢
  • 1970-01-01
  • 2016-07-05
  • 2015-01-03
  • 2018-12-30
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
相关资源
最近更新 更多