Skip to content

Instantly share code, notes, and snippets.

@lavesan
Last active October 23, 2019 00:57
Show Gist options
  • Save lavesan/40cf619de78bb89df02e01e370eafe4f to your computer and use it in GitHub Desktop.
Save lavesan/40cf619de78bb89df02e01e370eafe4f to your computer and use it in GitHub Desktop.
Linkar FormGroups Angular 6+
  • Componente Filho

typescript

export class ChildComponent implements OnInit {
  public childGroup: FormGroup;

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.childGroup = this.fb.group({
      control1: [''],
      control2: ['', [Validators.required]],
    });
  }
}
  • Componente Pai

html

<app-child-component></app-child-component>

typescript

export class FatherComponent implements AfterViewInit {
  @ViewChild(ChildComponent)
  childComponent: ChildComponent;
  
  public fatherGroup: FormGroup;
  
  constructor() {}
  
  ngAfterViewInit() {
    // Faz uma cópia do grupo no componente filho no grupo do componente pai
    this.fatherGroup = this.childComponent.childGroup;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment