简体   繁体   English

如何从Angular 2+中的父组件到达子组件的NgForm

[英]How to reach NgForm of child component from parent component in Angular 2+

There are multiple sibling component with different NgForm (template-driven forms) inside each of them.有多个同级组件,每个组件内部都有不同的 NgForm(模板驱动的表单)。 All of them are wrapped with a parent component.所有这些都被一个父组件包裹着。 Parent component has validation button.父组件具有验证按钮。 I need to validate certain child NgForm within click event on the button on a parent component.我需要在父组件上的按钮上的单击事件中验证某些子 NgForm。

What approaches to solve this would you propose?你会提出什么方法来解决这个问题?

// Parent template
<child id="0"></child>
<child id="1"></child>

// Parent component
validateChild(id: number) {
  // need somehow reach child component NgForm with a particular id
}

// Child Component template
<form #myForm="ngForm">...</form>

// Child Component template
@ViewChild('myForm') 
public myForm: NgForm;

When I am saying validation - I mean referring to following:当我说验证时 - 我的意思是指以下内容:

Object.keys(form.controls).forEach(key => {
   form.controls[key].markAsTouched();
})

I was thingking about following approach:我正在考虑以下方法:

// Parent component
stages = [
  {
    validated: false
  },
  {
    validated: false
  },
]
// Parent component view
<child id="0" [validate]="stages[0].validated"></child>
<child id="1" [validate]="stages[1].validated"></child>

// Child component
@Input()
public set validate(value: boolean) {
  if (value === true)
  Object.keys(this.myForm.controls).forEach(key => {
     this.myForm.controls[key].markAsTouched();
  })
}

But I don't really like it..Probably there is a better approach.但我真的不喜欢它..可能有更好的方法。 Because with this one without extra child EventEmitter I can't check whether form is valid or not因为这个没有额外的子 EventEmitter 我无法检查表单是否有效

If you want to have multiple forms for grouping, but not separate submit, then you could use FormGroup to track a related set of controls.如果您希望有多个表单进行分组,但又不想单独提交,那么您可以使用FormGroup来跟踪一组相关的控件。

So you can validate child components like that: 所以你可以像这样验证子组件:

@Component({
  selector: 'my-app',
  template: `
    <form [formGroup]="reactiveFormGroup">
      <input formControlName="foobar" />
      <my-component **[group]="reactiveFormGroup"**></my-comp>
    </form>

    form value: {{ reactiveFormGroup.value | json }}
  `
})
export class AppComponent { 
  reactiveFormGroup = new FormGroup({
    foo: new FormControl('default foobar'),
    bar: new FormControl('default foobar2')
  });
}

Child component code, eg my-component子组件代码,例如my-component

@Component({
  selector: 'my-component',
  template: `
    <div [formGroup]="group">
      <input   [formControlName]="'foobar2'" />
    </div>
  `
})
export class MyComponent { 
  @Input() group: FormGroup;
}

I have faced similar kind of issue, then I am following below approach我遇到过类似的问题,然后我遵循以下方法

Parent Component父组件

  @ViewChild(FirstChidComponent)
  public fChild: FirstChidComponent;

  validateChild(id: number) {
    console.log(this.fChild.myForm);
  }

you can access form values of the childComponent like this.fChild.myForm .您可以像this.fChild.myForm一样访问childComponent表单值。

you can also do the validation for the child Form您还可以对子表单进行验证

 form.controls[controlName].markAsTouched();

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

相关问题 在angular2中将父组件的值传递给ngForm的子组件? - Passing value from Parent component to child component for ngForm in angular2? 如何在ngSwitch的Angular 2+中将事件从子级绑定到父级组件 - How to bind event from child to parent component in Angular 2+ in ngSwitch 如何验证子组件 NgForms 与 Angular4 中的父组件有 3 个不同的 NgForm - How to validate the child component NgForms having 3 different NgForm from the parent component in Angular4 Angular 2+ 从其父组件调用子组件中的函数 - Angular 2+ call a function in a child component from its parent component Angular 2+将数据从子组件发送到父组件 - Angular 2+ Send data from child to parent component 父组件的子组件表单中的Angular 2+表单组setValue - Angular 2+ formgroup setValue in child component form from parent Angular 2+尝试从父组件到子组件输入数据 - Angular 2+ Attempting to input data from parent to child component 将 ngModel 从子组件传递到包含 ngForm 的父组件 - Pass a ngModel from a Child Component to a Parent Component containing the ngForm 带有验证的ngForm和角度为6的子组件 - ngForm with validation and child component in angular 6 如何在 Angular 2+ 中没有“父子”关系的情况下动态地将数据从一个组件传递到另一个组件? - How to pass data from one component to another dynamically without "Parent-Child" relationship in Angular 2+?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM