简体   繁体   English

在孩子中访问父母的 ngForm

[英]Access ngForm of parent in child

I have a structure like this:我有这样的结构:

<form #myForm="ngForm">
  <div class=row>
    <app-section-a [myForm]="myForm"></app-section-a>

and my child component:和我的子组件:

@Component({
...
  viewProviders: [ { provide: ControlContainer, useExisting: NgForm } ]
})
// ...
@Input() myForm: ElementRef;
<input ...>
<button type=submit [disabled]="!myForm.valid">

and, this works perfectly to ensure that all the components in the form are counted for validity.并且,这可以完美地确保表单中的所有组件都被计算为有效性。 The thing is, I don't think I should need to pass the reference to ngForm directly as an input.问题是,我认为我不需要直接将 ngForm 的引用作为输入传递。 How do I skip that input and still make this work?如何跳过该输入并仍然使这项工作?

a detail: happens to be an angular 5 project.一个细节:恰好是一个 angular 5 项目。

Complete working example in this StackBlitz Link .StackBlitz Link 中的完整工作示例。

First of all you do not need to pass ngForm to child component at all.首先,您根本不需要将ngForm传递给子组件。 Because you can tell child component is part of parent form group using ControlContainer and NgForm by providing to child-component providers array.因为您可以通过提供给子组件提供者数组来使用ControlContainerNgForm告诉子组件是父表单组的一部分。

In this example one parent component app.component is loading child component with *ngFor and we just added ControlContainer to providers array of child component.在这个例子中,一个父组件app.component正在使用*ngFor加载子组件,我们刚刚将ControlContainer添加到子组件的 providers 数组中。 Here, we just need to give different name for each child-component dynamically, for this purpose we need to pass index as @Input() to child component and assign it to name property of inputField.在这里,我们只需要为每个子组件动态赋予不同的名称,为此我们需要将 index 作为@Input()传递给子组件并将其分配给 inputField 的name属性。

If any of inputControl has error then, submit button of parent control is disabled.如果任何 inputControl 有错误,则父控件的submit按钮被禁用。

app.component.html is... app.component.html是...

<form #heroForm="ngForm" class="container">
   <div id="parent" *ngFor="let i of [0,1,2]"  >
      <app-child [id]="i+1" [name]="i+1" ></app-child>
   </div>
  <hr>
    {{heroForm.value | json}}
  <hr>
  <div>
     <app-button-submit></app-button-submit>
  </div>
</form>

app-button-submit.html app-button-submit.html

<button type="submit" class="btn btn-success"
  [disabled]="!control.form.valid">Submit</button>

We are binding , ControlContainer directly to [disabled] state of button with dependency injection.. see below constructor() class file.我们通过依赖注入将ControlContainer直接绑定到按钮的[disabled]状态..见下面的constructor()类文件。

app-button-submit.ts app-button-submit.ts

import {ControlContainer, NgForm} from '@angular/forms';

@Component({
   selector: 'app-button-submit',
   templateUrl: './button-submit.component.html',
   styleUrls: ['./button-submit.component.css'],
   providers : [{provide : ControlContainer, useExisting : NgForm}]
})
export class ButtonSubmitComponent  {
   constructor(private control : NgForm) { }
}

child-component.html子组件.html

<div class="form-group" >
   <label [for]="Name">Name</label>    
      <input type="text"  class="form-control"  [id]="id" required [(ngModel)]="vname" [name]="Name"  #names="ngModel">
        {{vname}}
   <div [hidden]="names.valid || names.pristine" class="alert alert-danger"> 
      Name is required  
   </div>
</div>

child.component.ts子组件.ts

import {ControlContainer, NgForm} from '@angular/forms';

@Component({
   ---,
   viewProviders: [ { provide: ControlContainer, useExisting: NgForm } ]
})
 export class ChildComponent implements OnInit {
   @Input('id') id;
   @Input ('name') Name;
 }

here, ControlContainer will take care of binding of NgForm of Parent-Component to Child-Component.在这里, ControlContainer将负责将父组件的NgForm绑定到子组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM