javascript - 如何以 react 形式绑定(bind),即 Angular 中的 formcontrolname 和 ngmodel

标签 javascript angular typescript

这是代码:

list.component.html

<form nz-form [formGroup]="taskFormGroup" (submit)="saveFormData()">
        <div nz-row *ngFor="let remark of checklis>
          <div nz-col nzXXl="12" *ngFor="let task of remark.tasks" style="padding: .5rem;">
<nz-form-item>
                  <nz-form-control>
                    <nz-radio-group formControlName="status" name="status" (change)="onChangeStatus($event)">
                      <label nz-radio nzValue="true">Passed</label>
                      <label nz-radio nzValue="false">Failed</label>
                    </nz-radio-group>
                  </nz-form-control>
                </nz-form-item>
</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"
        }
      ]
    }
  ];

constructor (private fb: FormBuilder) {}

createTaskFormGroup() {
    const DATA = this.asset;

    return this.fb.group({
      remark: DATA || '',
      status: ['', [Validators.required]]
    });
  }

onChangeStatus(ev: any) {
    console.log(ev);
}

如何解决

ERROR TypeError: Cannot assign to read only property 'id' of object '[object Object]'

选择通过或失败时,出现错误 Cannot allocate to read only property 'id' of object '[object Object]'

最佳答案

(change) 更改为 (ngModelChange) 事件。

<nz-radio-group formControlName="status" name="status" (ngModelChange)="onChangeStatus($event)">

文档: https://ng.ant.design/components/radio/en#nz-radio-group

关于javascript - 如何以 react 形式绑定(bind),即 Angular 中的 formcontrolname 和 ngmodel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59340908/

相关文章:

javascript - IE7 中的 HTMLElement 原型(prototype)

javascript - 根据内部div调整div的高度

javascript - Yandex Maps Api 示例不起作用

javascript - AngularJS 2 : How to have an 'attribute' directive communicate with its host component?

angular - 将 Observable 中的参数提供给返回 Observable 的函数

typescript - 将数组作为参数应用于 Date 构造函数

TypeScript:在运行时检查对象/缩小中的键

javascript - 如何在我的图像编辑器中根据输入的高度和宽度设置 Canvas 大小?

javascript - Angular 4 ngFor 设置元素的类

javascript - typescript 错误?或者只是 typescript 检查并不完美?