【问题标题】:How to use formGroupName inside child components如何在子组件中使用 formGroupName
【发布时间】:2019-05-04 10:01:10
【问题描述】:

如何在子组件中使用 formGroupName? 例如:

我有 ParentFormComponent

    parentForm: FormGroup;
    constructor(private fb: FormBuilder, private http: Http) { }
    ngOnInit() {


 this.parentForm = this.fb.group({
        _general: this.fb.group ({
           ProjectName:''
       })
  })
 }

在html中:

  <form [formGroup]="parentForm" (ngSubmit)="submitForm()">
     <div formGroupName="_general">
        <mat-form-field>
             <input matInput placeholder="Project name" 
             formControlName="ProjectName">
        </mat-form-field>
    </div> 
  </form> 

它工作得很好,但是当我想使用子组件时它不起作用:

<form [formGroup]="parentForm" (ngSubmit)="submitForm()">
          <app-child [parentForm]='parentForm'></app-child>
      </form> 

当我将它插入子组件时:

<div formGroupName="_general">
            <mat-form-field>
                 <input matInput placeholder="Project name" 
                 formControlName="ProjectName">
            </mat-form-field>
        </div> 

在ts文件中

 @Input() parentForm:FormGroup;

我遇到了错误: formGroupName 必须与父 formGroup 指令一起使用。您需要添加一个 formGroup 指令并将一个现有的 FormGroup 实例传递给它(你可以在你的类中创建一个)。

【问题讨论】:

  • 请参考this link,它可能会对您有所帮助。

标签: angular angular6 angular-reactive-forms


【解决方案1】:

使用 FormGroupDirective 代替使用输入属性绑定

FormGroupDirective

该指令接受现有的 FormGroup 实例。届时将 使用此 FormGroup 实例来匹配任何子 FormControl、FormGroup、 和 FormArray 实例到子 FormControlName、FormGroupName 和 FormArrayName 指令。

使用Viewproviders提供controlContainer,在你的子组件中注入FormGroupDirective获取父窗体实例

app.parent.html

<form [formGroup]="parentForm">
  <app-child></app-child>
</form>

child.component.ts

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

  ngOnInit() {

    this.childForm = this.parentF.form;
    this.childForm.addControl('_general', new FormGroup({
      ProjectName: new FormControl('')
    }))

  }
}

child.component.html

<div formGroupName="_general">
     <mat-form-field>
                 <input matInput placeholder="Project name" 
                 formControlName="ProjectName"> 
      <mat-form-field>  
 </div>

例如:https://stackblitz.com/edit/angular-ezqupz

【讨论】:

  • 有没有办法添加多个具有相同 formGroupName 的组件?
  • @billyjov 你说的多个组件是什么意思?
  • @Chellappanவ 我叉了你的:stackblitz.com/edit/angular-ynjdlm 这不起作用,因为我使用formGroupName。请参阅 app.component.html 以了解 [formGroup] 将如何工作,但 formGroupName 不会。
  • 您可以这样做作为解决方法:stackblitz.com/edit/angular-ssryuc
  • 如何使用这种方法对组件进行单元测试?你如何避免No provider for FormGroupDirective?我尝试了很多方法,但都没有奏效。
猜你喜欢
  • 1970-01-01
  • 2020-12-28
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 2019-08-26
  • 2022-01-13
  • 2020-04-29
相关资源
最近更新 更多