[英]Dynamic form is not working while using ng-container and ng-template
I am creating a dynamic form where I am getting the form fields form api.我正在创建一个动态表单,我在其中获取表单字段表单 api。 I am using
ng-container & ng-template
to reuse the formgroup multiple times but it's not working as expected.我正在使用
ng-container & ng-template
多次重用表单组,但它没有按预期工作。 When I am using usual div it's working as expected.当我使用通常的 div 时,它按预期工作。
https://stackblitz.com/edit/angular-ivy-hqc49t https://stackblitz.com/edit/angular-ivy-hqc49t
You can use FormGroup instances instead of formGroupName, because template loses context of formGroup: Stackblitz您可以使用 FormGroup 实例而不是 formGroupName,因为模板会丢失 formGroup 的上下文: Stackblitz
<ng-container
*ngTemplateOutlet="
fieldsTemplate;
context: {
formGroup: formGroundUse.get('groundUses')
}
"
>
</ng-container>
<div formArrayName="groundUsesArray">
groundUsesArray
<ng-container *ngFor="let data of groudUse.controls">
<ng-container
*ngTemplateOutlet="
fieldsTemplate;
context: {
formGroup: data
}
"
>
</ng-container>
</ng-container>
<ng-template #fieldsTemplate let-formGroup="formGroup">
<div [formGroup]="formGroup">
<tr>
<td class="input-container">
<label for="">title</label>
<input type="text" class="form-input" formControlName="title" />
</td>
<td class="input-container">
<label for="">value</label>
<input type="text" class="form-input" formControlName="value" />
</td>
<td class="input-container">
<label for="">percentage</label>
<input type="number" class="form-input" formControlName="percentage" />
</td>
</tr></div
></ng-template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.