0

I came across the following Stackblitz demo :

https://stackblitz.com/edit/angular-dffny7?file=app

with reference to the below post: https://stackoverflow.com/a/48527939/7429196

I need that when the user adds a new Company (e.g Company2), it should get created along with its respective project (say, Project2).

I am not getting the correct way to add nested formbuilder arrays in a formgroup. Can any one please tell me how I can do it?

1 Answer 1

1

You can try with this solution.

nested formbuilder arrays in a formbuilder group

Stackblitz Demo

  createProjectGroup(){
    return this.fb.group({
        projectName: ['']
      })
  }



addNewCompany() {
    let control = <FormArray>this.myForm.controls.companies;
    control.push(
      this.fb.group({
        company: [''],
        projects: this.fb.array([this.createProjectGroup()])
      })
    )
  }
Sign up to request clarification or add additional context in comments.

1 Comment

In the link which I shared, control has been passed as a parameter to add new project. How can I call addNewProject() from addNewCompany() so that when I create a new company, a new project gets created along with it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.