For a variety of reasons, I need to have a formArrayName directive applied to a div with as a child a ng-container with a ngTemplateOutlet.
Let me clarify it with HTML templates.
The following template will work
<div formArrayName="credentials" *ngFor="let creds of forArr.controls.credentials?.controls; let i = index">
<ng-container [formGroupName]="i">
<input placeholder="Username" formControlName="username" />
<input placeholder="Password" formControlName="password" />
</ng-container>
</div>
Whereas this one won't
<div formArrayName="credentials" *ngFor="let creds of forArr.controls.credentials?.controls; let i = index">
<ng-container [formGroupName]="i">
<ng-container *ngTemplateOutlet="x"></ng-container>
</ng-container>
<ng-template #x>
<input placeholder="Username" formControlName="username" />
<input placeholder="Password" formControlName="password" />
</ng-template>
</div>
The only difference is thus that I wrap all input code in a template.
A major reason I need this is that I would like to reuse the template content (with two inputs) for both FormGroupwith plain fields (Username and Password) as within Formarray's for a list of username and passwords.
NOTE I left out some code regarding [formGroup]="form"> and the internal buildup as this is irrelevant for the question. The code works just fine and all controls exists and are bound with the correct directives. I just would like to set NgTemplate's within the HTML template.
I try to reuse my templates as much as possible such that it can work within both groups as on an array within a group.