Angular ngTemplate和ngContainer中的FormControl
我有一个在两个条件内重复模板的工作角度表单,因此我将其移到了一个公共位置,并定义了ngContainer,通过传递变量来显示值,但是在模板内,代码无法理解表单,我得到了以下错误Angular ngTemplate和ngContainer中的FormControl,angular,angular-forms,ng-template,angular-formbuilder,Angular,Angular Forms,Ng Template,Angular Formbuilder,我有一个在两个条件内重复模板的工作角度表单,因此我将其移到了一个公共位置,并定义了ngContainer,通过传递变量来显示值,但是在模板内,代码无法理解表单,我得到了以下错误 BulkEditComponent.html:28 ERROR Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup directive and pass
BulkEditComponent.html:28 ERROR Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup
directive and pass it an existing FormGroup instance (you can create one in your class).
模板
<h5 mat-dialog-title>
Edit
</h5>
<div mat-dialog-content class="body p-10">
<div *ngFor="let item of keyList;let i=index" [formGroup]="editForm">
<div class="left" *ngIf="i%2 == 0">
<ng-container *ngTemplateOutlet="columnData;context:{item:item,group: editForm}"></ng-container>
</div>
<div class="right" *ngIf="i%2 == 1">
<ng-container *ngTemplateOutlet="columnData;context:{item:item,group: editForm}"></ng-container>
</div>
</div>
</div>
<mat-dialog-actions class="align-right">
<button mat-button mat-dialog-close>Cancel</button>
<button mat-flat-button color="primary" (click)="formatData()">Update</button>
</mat-dialog-actions>
<ng-template #columnData let-item='item' ;let-formGroup="editForm">
<div class="row p-2">
<div class="col-md-6">
{{getColumnName(item)}}
</div>
<div class="col-sm-6">
<input type="text" class="input-field" [formControlName]="item" />
</div>
</div>
</ng-template>
编辑
取消
更新
{{getColumnName(项)}}
在我的情况下,我无法获取formgroup niside ng模板,他们将formgroup放在模板本身中,而我需要从顶层共享它使用[formControl]=“editForm.get(item)”
而不是[formControlName]=“item”