Reputation: 222
I have a dynamics ng-select and input through an arrayForm.
HTML:
<ng-container formArrayName="bottles">
<div class="col">
<div class="productContainer">
<ng-container *ngFor="let bottle of getBottles.controls; let i=index">
<div [formGroup]="bottle" style="display: flex">
<label for="alias-{{ i }}">Produit:</label>
<ng-select id="productInput" (change)='selectTest($event, i)'
[items]="test[0] | keyvalue" bindLabel="value.name" bindValue="value.quantity" formControlName="product"></ng-select>
<label for="bottleQuantity">Bottles needed: </label>
<input *ngFor="let bott of bottles" style="height: 26px" id="bottleQuantity" type="bottleQuantity" formControlName="bottleQuantity"/><div for="alias-{{ i }}" style="color: green" *ngIf="itemSelected">(Qty: {{itemSelected}})</div>
<span class="material-icons" style="cursor:pointer; color: red" *ngIf="getBottles.controls.length > 1" type="button addClass" (click)="removeBottle(i)">delete</span>
</div>
</ng-container>
<span class="material-icons" style="cursor:pointer; color: green" (click)="addBottlesInput()">add_circle</span>
</div>
</div>
</ng-container>
What i'd like is to display the own quantity of bottles for each dynamic field using index. But i don't know how to perform this using index (or an easier way ? )
<div for="alias-{{ i }}" style="color: green" *ngIf="itemSelected">(Qty: {{itemSelected}})</div>
See an example here StackBliz
Thank you for help :)
Upvotes: 0
Views: 49
Reputation: 51240
As you want to show the quantity for the selected product per row:
public itemsSelected: any[] = [];
itemsSelected
array.public addBottlesInput = () => {
...
this.itemsSelected.push({});
};
itemsSelected
by index.public selectTest = (event, i) => {
if (event) {
event.index = i;
console.log('SELECTED', event);
this.itemsSelected[i] = event.value;
} else {
this.itemsSelected[i] = {};
}
};
itemsSelected
by index. Use ?.
(optional chaining) to safe get the quantity, especially for the case when the product is deselected.<div style="color: green">
(Qty: {{ itemsSelected[i]?.quantity }})
</div>
Or
<div style="color: green" *ngIf="itemsSelected[i]">
(Qty: {{ itemsSelected[i].quantity }})
</div>
Upvotes: 1