Issue
I have this form and I need to map the model to form array declaration.
formGroup: FormGroup;
builder: FormBuilder
data :{
lineItems : [
{
qty: null,
item: null
}
]
}
this.formGroup = this.builder.group({
lineItems: this.builder.array([
{
item: [this.lineItems.item, Validators.required],
}
])
});
I have doubts about this "item: [this.data.lineItems.item, Validators.required]" domain.lineItems is an array and how can I map it with each formbuilder array.
<div formArrayName="classmates">
<div *ngFor="let item of lineItems; trackBy: trackId; let i = index;">
<input
type="number"
class="form-control form-control-lg form-control-solid"
/>
</div>
</div>
Solution
What are you searching for is:
this.formGroup = this.builder.group({
lineItems: this.builder.array(
this.data.lineItems.map(line => {
return this.builder.group({item: [line.item, Validators.required]}
})
)
});
In that way, you have a formGroup inside of formArray
Answered By - Luca Angrisani
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.