[英]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.