Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当表单提交按钮位于父组件中时,如何将数据从子组件获取到父组件中?_Javascript_Angular_Angular7 - Fatal编程技术网

Javascript 当表单提交按钮位于父组件中时,如何将数据从子组件获取到父组件中?

Javascript 当表单提交按钮位于父组件中时,如何将数据从子组件获取到父组件中?,javascript,angular,angular7,Javascript,Angular,Angular7,我有两个部分: 父组件HTML: <parent-component> <child-component [input]="inputValue"> </child-component> <child-component [input]="inputValue"> </child-component> <button mat-stroked-button> Submit </button>

我有两个部分:

父组件HTML:

<parent-component>
    <child-component [input]="inputValue"> </child-component>
    <child-component [input]="inputValue"> </child-component>

    <button mat-stroked-button> Submit </button>
</parent-component>
在我的子组件html中,我有两个
输入:

<form [formGroup]="myForm">
    <mat-form-field>
           <input matInput formControlName="myInput">
      </mat-form-field>
     <mat-form-field>
           <input matInput formControlName="myInput2">
      </mat-form-field>
</form>


但是,当实际的submit按钮位于父组件中时,如何正确地从父组件中的子组件获取matInput值?

我将在子组件上创建一个
public getData(){}
函数,返回子组件的数据。父提交将类似于

public submit() {
    const data = this.childReference.getData();
    // do whatever you need to do with the data from child
}
在您的示例中,您似乎试图从多个子对象获取数据,在这种情况下,您必须使用


我也会考虑让父对象拥有一个AND,并重构组件,以便子组件是与该表单一起工作的自定义输入。请参阅在

中创建的名为状态选择器的自定义输入组件,我将在子组件上创建一个public getData(){}函数,返回子组件的数据。父提交将类似于

public submit() {
    const data = this.childReference.getData();
    // do whatever you need to do with the data from child
}
在您的示例中,您似乎试图从多个子对象获取数据,在这种情况下,您必须使用


我也会考虑让父对象拥有一个AND,并重构组件,以便子组件是与该表单一起工作的自定义输入。如果您的项目具有redux设置,请参阅在

中创建的名为状态选择器的自定义输入组件。你应该和redux一起去。Redux用于解决类似类型的问题。

如果项目中有Redux设置。你应该和redux一起去。Redux用于解决类似类型的问题。

在父级中提交并希望在子级中获得数据?如果是问题,我在子级中有一个输入,我希望在父级中使用“提交”按钮时在父级中获得这些输入值。您能提供stackblitz以便我可以查看?在父级中提交并希望在子级中获得数据?如果是问题,我在child中有一个输入,我想在submit按钮位于parent中时在parent中获取这些输入值。您能提供stackblitz以便我可以查看吗?使用
ViewChild@
我只从第一个孩子那里获取子信息。为什么,第二个孩子给我带来了空字符串..使用
ViewChild@
我只从第一个孩子那里得到了一个孩子的信息。为什么,第二个孩子给我带来了空字符串。。