2

See my plnkr: http://plnkr.co/edit/OSMtkjWGsDUz1wENtRDR?p=preview

I've got 3 components:

1) App: The base form. This holds references to "mini forms" i.e. subcontrols in the code

2) AppChild: These represent a single "mini form"

3) AppOption: These are a single input control in AppChild

You can click "Add Control" to add a new AppChild, and click "Click to add a new option +" to add an AppOption to an AppChild.

As you can see on my plnkr - each individual mini form (Sub control) has the correct form value printed out.

And the base form - can recognise the correct # of mini forms.

But how do I get it so the mini form values are contained in the base form values - instead of an empty array []?

I've added a comment on the plunkr where I think the issue is but I'm not sure if this is the reason/or how to fix it Line 49

initControl() {
return this.fb.array([]);
  }

2 Answers 2

4

You can create FormGroup instead of FormArray.

app.component.ts

initControl() {
  return this.fb.group({});
}

and then manipulate this group in your app-sub-form component

app-sub-form.component.ts

ngOnInit() {
  // We've already had FormGroup, so we can add control to it
  this.subForm.addControl('subOptions', this.fb.array([]));
  this.addOptions();
}

Plunker Example

Sign up to request clarification or add additional context in comments.

Comments

0

Not really a full solution, but start looking at this example. It may give you some ideas.

https://scotch.io/tutorials/how-to-build-nested-model-driven-forms-in-angular-2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.