您需要在父项中构建整个表单,然后将内部FormGroups传递给子项和孙项。因此,父建全的形式:
ngOnInit() {
this.myForm=this._fb.group({
subAppName: ['', [Validators.required]],
vendorDetails: this._fb.group({
buildType: [''],
primaryLang: [''],
product: this._fb.group({
vendorName: [''],
productName: ['']
})
}),
subAppType:['', [Validators.required]],
});
}
从你的父母,在vendorDetails组传递给供应商的组件:
<subapp-vendor #vendortest [vendorDetails]='myForm.controls.vendorDetails'></subapp-vendor>
和供应商使用@input为formgroup:
@Input() vendorDetails: FormGroup;
并在您的视图中使用该formGroup名称以及您在父级中定义的formcontrolnames。在这里你还内formGroup传递给此childcomponent的子组件,就像你从父所做的:
<div [formGroup]="vendorDetails">
<label>Built Type</label>
<div class="radio" *ngFor="let buildType of buildTypes">
<label>
<input type="radio" formControlName="buildType" [value]="buildType.value">
{{buildType.display}}
</label>
</div>
<subapp-product #producttest [product]="vendorDetails.controls.product"></subapp-product>
<label>Primary Language</label>
<input type="text" class="form-control" formControlName="primaryLang">
</div>
aaaand那当然使用@input和formGroup product
在产品组件的观点,就像以上。
这是你的forked plunker
使用'@ output'装饰http://learnangular2.com/outputs/ – Smit
您可以更新根据您的plunker例如您的文章**代替父..子。 child1 .. ** – Aravind