【发布时间】:2019-09-27 05:58:36
【问题描述】:
我有两个组件:
父组件 HTML:
<parent-component>
<child-component [input]="inputValue"> </child-component>
<child-component [input]="inputValue"> </child-component>
<button mat-stroked-button> Submit </button>
</parent-component>
父组件TS: 在这里,我试图测试 ViewChild wokrs 是否正确。我从父组件中的子组件获取属性值。
export class ParentComponent implements OnInit {
@ViewChild(ChildComponent) childReference;
parentString: string;
constructor(
private cdRef: ChangeDetectorRef,
) { }
ngOnInit() {
}
ngAfterViewInit() {
this.parentString = this.childReference.exampleChild;
this.cdRef.detectChanges();
}
在我的子组件 html 中,我有几个 <mat-form-field> 输入:
<form [formGroup]="myForm">
<mat-form-field>
<input matInput formControlName="myInput">
</mat-form-field>
<mat-form-field>
<input matInput formControlName="myInput2">
</mat-form-field>
</form>
但是当实际提交按钮在父组件中时,如何正确地从父组件中的子组件获取 matInput 值?
【问题讨论】:
-
在 Parent 中提交并希望 Child 中的数据?
-
如果是一个问题,我在子项中有一个输入,当提交按钮在父项中时,我想在父项中获取这些输入值。
-
你能提供 stackblitz 让我看看吗?
标签: javascript angular angular7