Skip to content
Advertisement

update the quantity without creating a new row with angular as simply as possible

I try to do a shopping cart each time I add the same item it creates a new line for me, I would like to update the Quantity, do I have to make a loop that goes through an array?

ts.file

JavaScript

html

JavaScript

Advertisement

Answer

You can change your add(product) to:

JavaScript

Here it will search for a similar product (I dont know which was the unicity criteria, so I compared the whole object with the whole new added one), If it’s found, it would update the quantity, else it push a new product.

And the HTML part:

JavaScript

DEMO

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement