You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.7 KiB

Parent Component Creates Form and Passes Form Controls Into Child Components

An alternative approach to refactoring a component into child sub-components is to make the parent component be responsible for creating the entire Reactive Form, and to pass the appropriate form controls into the children. By also refactoring the logic for creating the form into a builder method in a separate service, the parent control’s logic and template become almost as simple as in the Own Form Controls application:

export class AppComponent implements OnInit, OnDestroy {
  contact: Contact;
  form: FormGroup;

  private subscription: Subscription;

    private service: ContactService,
    private formService: ContactFormService,
  ) {}

  public ngOnInit() {
    this.subscription = this.service
      .subscribe((data: Contact) => { = data;
        this.form = this.formService.createForm(data);
<form [formGroup]="form">

While the templates for nested-forms-name and nested-forms-address-list using this approach are almost identical to the templates in the Own Form Controls example, the typescript code is even simpler:

  selector: 'nested-forms-name',
  templateUrl: './name.component.html',
  styleUrls: ['./name.component.css']
export class NameComponent {
  @Input() nameGroup: FormGroup;

Form Builder Service

When looking at Reactive Forms through the lens of the Model, View, Controller pattern, the FormGroup is the Model, and the HTML templating is the View. By using factory service methods to create the overall form structure, one can gain the benefits of encapsulation and composition while maintaining a strong separation between the Model and the View.

import { Injectable } from '@angular/core';
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
import { Contact, Name, Address } from '@nested-forms/contact';

  providedIn: 'root',
export class ContactFormService {
  constructor(private fb: FormBuilder) {}

  public createForm(model: Contact): FormGroup {
      name: this.createContactNameForm(,
      addresses: this.createContactAddressListForm(model.addresses),

  public createContactNameForm(name: Name): FormGroup {
      firstName: [name ? name.firstName : ''],
      lastName: [name ? name.lastName : ''],
      middleName: [name ? name.middleName : ''],
      prefix: [name ? name.prefix : ''],
      suffix: [name ? name.suffix : ''],

  public createContactAddressListForm(addresses: Address[]): FormArray {
    const list: FormArray = this.fb.array([]);

    if (addresses) {
      addresses.forEach(addr => {

    return list;

  public createContactAddressForm(addr: Address): FormGroup {
      line1: [addr ? addr.line1 : ''],
      line2: [addr ? addr.line2 : ''],
      city: [addr ? : ''],
      state: [addr ? addr.state : ''],
      postalCode: [addr ? addr.postalCode : ''],


  • Clean separation between Model and View
  • The parent component is easy to understand and maintain
  • The child components are likewise easy to understand and maintain
  • Encapsulation and composition are provided by the factory methods


  • Having a service with factory methods adds complexity to the application