无法读取未定义的属性“ setParent”

时间:2018-12-05 09:54:11

标签: angular angular6 angular-reactive-forms

我使用的是角6反应形式,并且在子组件中有一个表格:

<div formArrayName="_server">
<table id="_server" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>Host name</th>
            <th>IP</th>
            <th>Domain</th>
            <th>OS</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>

        <tr *ngFor="let server of serverForms.controls; let i=index" [formGroupName]="i">
            <td>
                <input formControlName="HostName" class="form-control" type="text" />
            </td>
            <td>
                <input formControlName="IPAddress" class="form-control" type="text" />
            </td>
            <td>
                <input formControlName="Domain" class="form-control" type="text" />
            </td>
            <td>
                <input formControlName="OS" class="form-control" type="text" />
            </td>
            <td class="buttonCenter">
                <button mat-raised-button color="primary" class="deleteFieldButton" (click)="deleteServer(i)" type="button">delete</button>
            </td>
        </tr>
    </tbody>
    <button mat-raised-button color="primary" class="addFieldButton" (click)="addServer()" type="button">Add new server</button>

</table>

我正在从父母那里将表格传递给孩子。

您可以在ts文件中看到

  childForm;
  constructor(private parentF: FormGroupDirective) { }

  ngOnInit() {

    this.childForm = this.parentF.form;
    this.childForm.get('_server') as FormArray;

}

  get serverForms() {
    return this.childForm.get('_server') as FormArray
  }

  addServer() {

    const server = this.childForm.addControl('_server', new FormGroup({
      HostName: new FormControl([]),
      IPAddress: new FormControl([]),
      Domain: new FormControl([]),
      OS: new FormControl([]),

    }))

    this.serverForms.push(server);
  }

  deleteServer(i) {
    this.serverForms.removeAt(i)
  }

deleteServer方法运行良好,但addServer()无法运行-我收到错误消息:无法读取未定义的属性'setParent'。

请指教! 谢谢!

2 个答案:

答案 0 :(得分:0)

我不使用FormGroupDirective做FormArray就这样

组件

  childForm: FormArray;
  constructor() {
    this.childForm = new FormArray([]);
  }

  addServer() {
    this.childForm.push(new FormGroup({
      HostName: new FormControl(),
      IPAddress: new FormControl(),
      Domain: new FormControl(),
      OS: new FormControl(),
    }));
  }

  deleteServer(i) {
    this.childForm.removeAt(i)
  }

这就是我在模板中所做的更改

<div formArrayGroup="childForm">

NgFor

<tr *ngFor="let frm of childForm.controls; let i=index" [formGroup]="frm">

stackblitz demo

答案 1 :(得分:0)

尝试此操作

parent.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormArray, FormBuilder } from '@angular/forms';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  name = 'Angular';
  form;
  constructor() {
    this.form = new FormGroup({
      arr: this.buildArray()
    })
  }
  ngOnInit() {
    console.log(this.form.get('arr').value);
  }
  get arr() {
    return this.form.get('arr').value;
  }
  buildGroup() {
    return new FormGroup({
      HostName: new FormControl([]),
      IPAddress: new FormControl([]),
      Domain: new FormControl([]),
      OS: new FormControl([]),
    })
  }
  buildArray() {
    return new FormArray([this.buildGroup()])
  }
  deleteServer(i) {
    this.form.get('arr').removeAt(i)
  }
  trackByFn(index: any, item: any) {
    return index;
  }
}

child.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormArray, ControlContainer, FormGroupDirective } from '@angular/forms';
@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.css'],
  viewProviders: [{ provide: ControlContainer, useExisting: FormGroupDirective }]
})
export class TableComponent implements OnInit {
  childForm;
  constructor(private parentF: FormGroupDirective) { }

  ngOnInit() {

    this.childForm = this.parentF.form;
    this.childForm.get('_server') as FormArray;
  }
  get serverForms() {
    return this.childForm.get('arr') as FormArray
  }

  add() {
        this.childForm.get('arr').push(this.buildGroup());     
  }

  buildGroup() {
    return new FormGroup({
      HostName: new FormControl(''),
      IPAddress: new FormControl(''),
      Domain: new FormControl(''),
      OS: new FormControl(''),
    })
  }    
}

示例:https://stackblitz.com/edit/angular-kin9jq