Next to this post I can post this result ["1", "2", "3"] but I have another problem.
When I select product, in view show id, 1, 2 and 3, not name of product. like in image
I change function updateForm()
like below:
products: Products[] = [];
updateForm(ev: any, idd: any, componentid: any) {
if (ev.isUserInput) {
if (componentid === 'products_name') {
this.prodId = idd;
for (let i = 0; i < this.products.length; i++) {
if (this.products[i].products_id=== idd) {
console.log(this.products[i].products_name) //in this part I can see product name
this.myForm.controls.products_id.setValue(this.products[i].products_name)
}
}
} else {
console.log('error');
}
}
}
And my html code is Like this:
<div class="row">
<div class="input-field col s10">
<div class="form-group">
<div formArrayName="products_id">
<div *ngFor="let p of myForm.get('products_id').value; let i = index">
<input formControlName="{{i}}" id="products_id" type="text" aria-label="Number" matInput
[matAutocomplete]="auto">
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete" [displayWith]="displayWith">
<mat-option (onSelectionChange)="updateForm($event, pro.products_id, 'products_name')" *ngFor="let pro of filteredProduct | async"
[value]="pro.products_id">
{{pro.products_name}}
</mat-option>
</mat-autocomplete>
<div class="button-left">
<button *ngIf="myForm.controls.products_id.value.length > 1" type="button" class="fa" (click)="onRemoveItem(i)">-</button>
</div>
</div>
</div>
</div>
</div>
<div class="input-field col s2">
<button type="button" class="btn" (click)="onAddItem()">+</button>
</div>
</div>
Any idea please? How to display products_name ?
Thank you!
There was an issue with your Template and with your
updateFormProducts
method.Here are the fixes:
In this method, you'll need to pass index of the State field in your
FormArray
and then callpatchValue
on only that field which you'll be able to get using(<FormArray>this.myform.get('products_id')).at(index)
Then in your template, you also need to fix a few things. Here's your updated template:
Here's an Updated StackBlitz for your ref.
UPDATE
For your issue, you might want to do this:
In template create a
getter
:And in the template, you can use
productIdFormArray
to iterate over through theFormArray
:You can find the changes in the Updated StackBlitz as well.