Rayner Kristanto
Rayner Kristanto

Reputation: 13

Angular6 - Component in Array Not Updating

Here is code showing the bug: https://stackblitz.com/edit/angular-gbn3kr

I am trying to dynamically update a total quantity field. I created a Quantity Component which is an input that takes in a number and stores that number. In my app component, I have an array of these Quantity Components. When I try QuantityComponent.getQuantity() from the app component, it is always returning 0 even though I know I am correctly setting the components quantity because the components quantities are correctly displaying.

I assume that this is because my QuantityComponent in the array is not being updated. Is my assumption right? How would you code this?

Upvotes: 1

Views: 145

Answers (1)

Avij
Avij

Reputation: 694

It is because your are creating new object of Quantity in your service. And in the View of App, you are Initiating two other independent instances of quantity. Whenever you use the component selector <quantity...>(like you did in app-component-html) angular instantiates the component. I have resolved it in this stackblitz

But you can enhance this by using EventEmitters which would better up the performance. I tried to stick with the idea that you had used that is why you would find a For-loop to get TOTAL value, but prefered way would be EventEmitters.

Upvotes: 1

Related Questions