Angular 在子组件中传递formGroup(角度5)

Angular 在子组件中传递formGroup(角度5),angular,angular5,Angular,Angular5,我想创建一个子组件,它接受formGroup并添加验证,但我收到一个错误,因为子组件没有察觉到它在父级的“表单”中 我怎样才能解决这个问题 父组件: <form [formGroup]="formulario"> <app-campo [(propiedad)]="usuario.correo" placeholder="title" email=

我想创建一个子组件,它接受formGroup并添加验证,但我收到一个错误,因为子组件没有察觉到它在父级的“表单”中

我怎样才能解决这个问题

父组件:

<form [formGroup]="formulario">
    <app-campo [(propiedad)]="usuario.correo"
                               placeholder="title"
                               email="true"
                               [(formulario)]="formulario"
                    ></app-campo>
  </form>
<h3>Label</h3>
    <input
           placeholder="{{placeholder}}"
           [(ngModel)]="valorPropiedad"
           name="campo"
           (ngModelChange)="cambiar($event)"
           formControlName="campo"
           type="{{tipoInput}}">


在子组件中,如果不指定
formGroup
,则无法指定
formControlName
,因此您需要做的是在子组件中添加formGroup:

子组件代码:

<div [formGroup]="formulario">
      <h3>Label</h3>
      <input
           placeholder="{{placeholder}}"
           [(ngModel)]="valorPropiedad"
           name="campo"
           (ngModelChange)="cambiar($event)"
           formControlName="campo"
           type="{{tipoInput}}">
</div>

标签
关于,请尝试以下解决方案:

正在将父组件“formGroup”与子组件连接 “formControlName”

在父零部件模板中

<form [formGroup]="recipeForm">
<app-recipe-name [parent]="recipeForm"></app-recipe-name>
</form>
<mat-input-container fxFill [formGroup]="parent">
   <input matInput placeholder="Recipe Name" [matAutocomplete]="recipeAutocomplete" formControlName="recipeName">
</mat-input-container>
在子组件模板中

<form [formGroup]="recipeForm">
<app-recipe-name [parent]="recipeForm"></app-recipe-name>
</form>
<mat-input-container fxFill [formGroup]="parent">
   <input matInput placeholder="Recipe Name" [matAutocomplete]="recipeAutocomplete" formControlName="recipeName">
</mat-input-container>

现在可以从父级访问“recipeName”

作者: