First of all, I've read a lot of stackoverflow's answers about nested forms but I couldn't fix my problem with those, thanks for your time.
I'm trying to do a form to edit user's information, the information I use is something like that:
(2) [Object, Object]
0: Object
mail: "example1@exp.exp"
1: Object
mail: "example2@exp.exp"
In my component I create a nested form:
// blablabla.component.ts
// imports, component, ...
export class EditMailsComponent implements OnInit {
@Input() data: Array<any>;
private form: FormGroup;
constructor(
private formBuilder: FormBuilder
)
ngOnInit(){
this.form = this.formBuilder.group({
mails: this.formBuilder.array([]),
});
this.fnOpen();
}
private fnOpen()
{
// ...
const control = <FormArray>this.form.controls['mails'];
this.data.forEach(x => {
control.push(this.populateForm(x);
});
// ...
}
private populateForm(x: Array<any>)
{
return this.formBuilder.group({
mail: [x.mail]
});
}
}
And my html file looks like this:
<!-- ... -->
<form class='smart-form' novalidate>
<div class='modal-body'>
<div [formGroup]='form'>
<div formArrayName='mails'>
<div *ngFor='let mail of form.controls.mails.controls; let i=index'>
<div [formGroupName]="i">
<fieldset>
<section class='col-col-6'>
<label class='input' id='mail'>
<input type='text' formControlName='mail' ngModel>
</label>
</section>
</fieldset>
<!-- ... -->
When I try to open the page to edit, the correct number of nested forms are created (ex: if i have a 4 object array, 4 forms are opened) but "mail" is not displayed in .
Could someone tell me where I'm missing?
If you need any more information, I'll be glad to help.
Update:
Places I've visited before ask:
- STACKOVERFLOW - Angular 2 Form “Cannot find control with path”
- STACKOVERFLOW - Angular 2 Form “Cannot Find Control”
- STACKOVERFLOW - Cannot find control with path: angular2
- STACKOVERFLOW - Angular 2 Accessing Nested FormArrays using FormBuilder
Tutorials (I tried to use those to find the error in my code):