我正在創建一個動態表單,我在其中獲取表單欄位表單 api。我ng-container & ng-template曾經多次重用表單組,但它沒有按預期作業。當我使用通常的 div 時,它按預期作業。
https://stackblitz.com/edit/angular-ivy-hqc49t
uj5u.com熱心網友回復:
您可以使用 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491845.html
標籤:javascript 有角度的 打字稿 角反应形式
