0

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>
1
  • I don't get what is your question. Do you have an error with the code you have, are searching for something better, etc..? Please provide more information Commented Oct 24, 2022 at 10:19

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.