The process to create a FormArray of FormArrays is equal that other. The only is take account how referred to the formArrays. So, first create two auxiliar functions
//it's a typical getter
//if you manage the formArray directly it's not necesary(*)
get itemsArray()
{
return this.form.get("items") as FormArray
}
//see that this function is NOT a getter -has as argument an index-
getTasks(index:number)
{
this.itemsArray.at(index).get('tasks') as FormArray
}
And, in case the outer formArray a typical function that return a formGroup
setItemsArray(data: any = null): FormGroup {
data = data || { templatename: null, tasks: null };
return new FormGroup({
templatename: new FormControl(data.templatename, Validators.required),
tasks: data.tasks
? new FormArray(data.tasks.map(x => new FormControl(x)))
: new FormArray([])
});
}
So, we can create the formArray using
const items=new FormArray(alldata.map(x=>this.setItemsArray(x))
Now, we are ready to mannage the formArray
<form [formGroup]="form">
<div formArrayName="items">
<div *ngFor="let group of itemsArray.controls;let i=index"
[formGroupName]="i">
<!--here we has a inner formGroup--->
<input formControlName="templatename">
<div formArrayName="tasks">
<div *ngFor="let control of getTasks(i).controls;let j=index">
<input [formControlName]="j">
</div>
</div>
</div>
</div>
</form>
BONUS:
To add/remove a new task in a position it's only
addTask(index:number)
{
this.getTask(index).push(new FormControl())
}
removeTask(index:number,indexTask:number)
{
this.getTask(index).removeAt(indexTask)
}
To add/remove a item
addItem()
{
this.itemsArray.push(this.setItemsArray())
}
removeItem(index:number)
{
this.itemsArray.removeAt(index)
}
(*)If you want, you can also mannage teh formArray standalone -not in a FormGroup-
if you define directly
this.formArray=new FormArray(alldata.map(x=>this.setItemsArray(x))
And change the function getTasks(index) by
getTasks(index)
{
return this.formArray.at(i).get('tasks') as FormArray
}
You can use a form like
<form [formGroup]="formArray">
<div *ngFor="let group of formArray.controls;let i=index"
[formGroupName]="i">
<!--here we has a inner formGroup--->
<input formControlName="templatename">
<div formArrayName="tasks">
<div *ngFor="let control of getTask(i).controls;let j=index">
<input [formControlName]="j">
</div>
</div>
</div>
</form>
You can see a stackblitz