Reputation: 11
i create stackblitz project
If i pass formcontrolname to ng-template using ngtemplateoutlet it is showing Error
Console was cleared 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).
Example:
`<div [formGroup]="myGroup">
` In your class:this.myGroup = new FormGroup({ firstName: new FormControl() });
if i use formControl i am getting another error..
i need to dynamicaly pass the formcontrolname
Upvotes: 1
Views: 443
Reputation: 57941
You can pass the own control
<ng-container *ngTemplateOutlet="dynamicCtrl;
context:{data:{control:testForm.controls?.age,label:'Age'}}">
</ng-container>
<ng-template #dynamicCtrl let-data="data">
<label>{{data.label}}</label>
<input [formControl]="data.control" />
</ng-template>
If you has a variable
field="age"
You can use
<ng-container *ngTemplateOutlet="dynamicCtrl;
context:{data:{control:testForm.get(field),label:'Age'}}">
</ng-container>
Upvotes: 0