we experiment some error when we try to use formGroup. I need some explanations about form group. I want create a form group with data. The data need to display row by row. we use angular 8 when I run it, I got this error :
No value accessor for form control with path: 'rowDetails -> 0 -> no'
I have this constructor of Componnent:
constructor(
public fb: FormBuilder
) {
this.summaryForm = this.fb.group({
rowDetails: this.fb.array([])
})
}
I have this controller of Componnent:
ngOnInit() {
this.productList = (this.selectedReceiptBatch.incomeSummary as SaleSummary).sales;
this.summaryForm = this.fb.group({
rowDetails: this.fb.array(
this.productList.map(x=> this.fb.group({
'no': x.product.code,
'desc': x.product.productClass,
'numberx': [x.number.x],
'numbery': x.number.y,
'amountz': x.number.z,
'reference': [x.reference]
})
))
});
console.log(this.summaryForm);
this.formControlAvailable = true;
}
HTML :
<div *ngIf="formControlAvailable">
<div>
<div [formGroup]="summaryForm">
<div formArrayName="rowDetails">
<div *ngFor="let row of summaryForm.controls.rowDetails.controls; let index = index">
<div [formGroupName]="index">
<div>
<span formControlName="no" >{{row.value.no}}</span>
</div>
<div>
<span *ngIf="row?.product?.code" formControlName="desc" >
{{row.value.desc}}
</span>
<div *ngIf="!row?.product?.code" ngbDropdown class="d-inline-block">
<mat-form-field>
<mat-label>Select an option</mat-label>
<mat-select [(value)]="row.product.code" formControlName="desc" >
<mat-option *ngFor="let product of cashableProducts" [value]="product.code">
{{product.code}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</div>
<div >
<input type="number" [value]="row.amount.net"
(change)="row.amount.net = parseNumber($event.target.value);calculate()"
formControlName='numberx' />
</div>
<div >
<span formControlName='numbery' >{{ row.value.numbery }}$</span>
</div>
<div >
<span formControlName='numberz' >{{ row.value.numberz }}$</span>
</div>
<div >
<input formControlName='reference' type="text" class="form-control" minlength="0" [(ngModel)]="row.reference" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You can put formControlName
directive only on input element, select, textarea or component that implements the interface value accessor interface.
Change this
<span formControlName="no" >{{row.value.no}}</span>
to input element like this
<input type="text" formControlName="no" >
If you just want to display the no
value
{{row.value?.no}}