I am writing a checkout view of a shopping cart where an array is displayed with the items to be purchased like this:
app.component.html
<div *ngFor="let x of shoppingarray">
<!-- Other code here... -->
</div>
I want to give the user the possibility of adding more items to the list at the last minute but somehow the total doesn't refresh even though I update the array and the subtotals.
I do update the subtotals on the array by incrementing a quantity var which affects directly the objects in the array like this:
app.component.ts
AddmoreItemsOFThistype(a) {
this.shoppingarray[a].qt+= 1;
}
Then I recalculate each subtotal by passing each subtotal through a pipe giving as parameter the x.qt which is the incremented quantity (the pipe just multiplies x.price * x.qt).
app.component.html
<div> {{ x.price | totalPerItem: x.qt }} </div>
The problem is that the global total won't update values that have been returned from the filters.
app.component.html
<div> {{ shoppingarray | CalculateTotalpipe }} </div>
CalculateTotalpipe just loops the whole array adding all totals, somehow the initial total is correct but modifications to the subtotals don't take effect.
Is there any way to refresh shoppingarray so that CalculateTotalpipe generates an updated total?
Is there a better approach to this?
Thanks!
{{shoppingarray|CalculateTotalpipe}}. Any suggestion on how to combine the[(ngModel)]with the var total and the filter alltogether? . I