如何从其父级组件和祖父级组件触发angular 5反应形式提交事件?

时间:2018-12-03 05:03:10

标签: angular angular-reactive-forms angular-components

我有三个组件-L1,L2和L3嵌套,如下所示: Component hierarchy

组分L3具有反应形式。我需要使用组件L2和L1中存在的按钮提交此表单。

我尝试使用@Viewchild,但无法正常工作。 Here is what I have tried

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题并解决了问题,但是创建了提交表单的公共方法

public submitForm() {
  this.myForm.submitted = true;
}

<form [formGroup]='myForm' (ngSubmit)="submitForm()">
  // ...
</form>

然后通过在父组件和祖父母组件中创建子组件的引用变量并使用该ref变量,我可以将其称为

this.child.submitForm();
const valid = this.child.myForm.valid;
console.log(valid);

这应该可以解决您的问题。