0

I am making an anonymous formArray input(autocomplete) to find item by using autocomplete.

After adding an autocomplete, the autocomplete input doesn't work though it works fine independently.

The error message is ERROR Error: Cannot find control with path: 'orderItems -> 0 -> search'

Could you help me improve the codes?

Thank you.

 <form [formGroup]="orderForm">
    <div formArrayName="orderItems">
      <div *ngFor="let orderItem of getControls(); let i = index" [formGroupName]='i'>
        <p-autoComplete
        formControlName="search"
        (onSelect)="onSelect($event)"
        [suggestions]="list">
        </p-autoComplete>
      </div>
    </div>
    <p-button (click)="onAdd()"> add </p-button>
</form>
public orderItems = new FormArray([])
public orderForm: FormGroup;

ngOnInit(): void {
    this.orderForm = new FormGroup({
      'orderItems': this.orderItems,
      search: this.fb.array([
        this.fb.control('')
      ])
    });

    this.orderForm
    .get('search')
    .valueChanges.pipe(debounceTime(1000))
    .subscribe((value) => {
      if(value === '') {return null};
      this.getList(value)
    })
}

  getControls() {
    return (<FormArray>this.orderForm.get('orderItems'))
    .controls;
  }

  get search() {
    return this.orderForm.get('search') as FormArray;
  }
5
  • 1
    The control 'search' is independent from 'orderItems'. Your autocomplete is inside of a ngFor that iterates over each orderItem. When inside of the iteration, you should access items contained within the object of orderItem. Basically, 'search' doesn't exist inside of orderItems. Commented Jun 26, 2021 at 2:53
  • @BrianSmith Thanks, Brian. Well noted. I thought the autocomplete is contained because it is inside of ngFor. So could you help me the codes working? Commented Jun 26, 2021 at 3:02
  • Can you set this up in a stackblitz and I will fork it. Commented Jun 26, 2021 at 3:10
  • @BrianSmith Thanks you so much. stackblitz.com/edit/angular-ivy-somt3x?file=src/app/… Commented Jun 26, 2021 at 4:02
  • 1
    Your stackblitz wouldn't compile for me and had too many errors. I put this together as a example of how to set up the form array with a search control and put a control on the page. It should get you in the right direction. stackblitz.com/edit/… Commented Jun 26, 2021 at 4:37

1 Answer 1

1

Set up your form initially with an empty array

buildForm() {
    this.orderForm = this.fb.group({
      orderItems: this.fb.array([]),
    });
  }

Then get the data for the array and populate the array

setOrderItemArray() {
    const orderItemsArray = this.orderForm.get('orderItems') as FormArray;
    this.orderItemsList.forEach(item => {
      orderItemsArray.push(this.buildOrderItemsForm(item))
    });
  }

  buildOrderItemsForm(item): FormGroup {
    return this.fb.group({
      id: item.id,
      search: item.search,
      name: item.name
    })
  }

Now you have an instance of 'search' in each object of the array and can use search autocomplete independently for each.

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

1 Comment

I'm still getting the error so for me it don't works.

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.