let's say I have the following form structure:
this.myForm = this.formBuilder.group({
date: ['', [Validators.required]],
notes: [''],
items: this.initItems()
});
initItems() {
var formArray = this.formBuilder.array([]);
for (let i = 0; i < 2; i++) {
formArray.push(this.formBuilder.group({
name: ['', [Validators.required]],
age: ['', [Validators.required]],
}));
}
return formArray;
}
and the name control supposed to be an autocomplete, how can I relate the all the name controls to an autocomplete list?
I solved this by relating each
name
control inside theFormArray
to afilteredOption
array:Then After each time I build a
formgroup
inside theform Array
(create new item), I need to call the above function at the new index like this:In the .html file, we need to refer to the desired filteredOption array, we can do this by using the
i
index:please see the detailed answer here https://stackblitz.com/edit/angular-szxkme?file=app%2Fautocomplete-display-example.ts