【问题标题】:How to use FormArray in child component with methods inside parent如何在子组件中使用 FormArray 和父组件中的方法
【发布时间】:2018-12-04 06:24:46
【问题描述】:

我正在构建具有反应形式的 Angular 6 形式,并且我正在为每个部分使用组件,但我遇到了一些问题: 如何在子组件中使用 FormArray 和父组件中的方法。 例如:

在父 ts 文件中:

  constructor(private fb: FormBuilder, 
    private http: Http) { }

  ngOnInit() {

    this.parentForm = this.fb.group({
  _server: this.fb.array([this.initItemRows()]),
   })
 }


  initItemRows() {
    return this.fb.group({
      // DocumentID:  uuid(),
      HostName: [],
      IPAddress: [],
      Domain: [],
      OS: []
    });
}


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

  addServer() {

    const server = this.fb.group({ 
      // DocumentID:  uuid(),
      HostName: [],
      IPAddress: [],
      Domain: [],
      OS: []
    })

    this.serverForms.push(server);
  }

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

在我的父 html 中:

<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">insert row</button>

    </table>
</div>

但我想将父级(上)的所有 html 代码用于子组件中

 <app-servers-section></app-servers-section>

我知道我可以在子组件中使用 FormGroupDirective,但是当我需要使用父方法时它不起作用。

请指教! 谢谢!

【问题讨论】:

标签: angular angular6 angular-reactive-forms formgroups


【解决方案1】:

你需要做这样的事情。

 <app-servers-section (changeInChildForm)="doSomething($event)"></app-servers-section>

在要保留表单的子组件中,您需要从那里发出一个事件。

所以,在您的 ChildComponent.ts 中导入 EventEmitterOutput 以及 Component 和其他东西

import { Component, EventEmitter, Input, Output } from '@angular/core';

在 Class 的主体中,您可以执行类似的操作来发出要从子级传递给父级的事件。

@Output() changeInChildForm = new EventEmitter<boolean>();

  somethingHappenedInChild(data: any) {
    this.changeInChildForm.emit(data);
  }

因此,当您想从子组件发出更改时,请调用子组件的方法somethingHappenedInChildchangeInChildForm 将发出,它将调用父组件的方法doSomething($event)

我没有创建堆栈闪电战。但是,如果您在这方面需要更多帮助,请像前面提到的其他人一样创建一个 stackblitz。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-12-08
  • 2020-06-15
  • 2021-09-24
  • 2022-10-04
  • 2020-12-28
  • 2020-05-19
  • 2022-01-12
相关资源
最近更新 更多