D
D
Danya Kozlovskiy2021-03-25 13:14:14
Angular
Danya Kozlovskiy, 2021-03-25 13:14:14

How to change the value of a property in an iterable object?

Hello, I'm looping through an array and displaying objects from there. How to bind to the buttons increase / decrease the value ( item.count) of the corresponding property of the object.

Code example:

<div *ngFor="let item of items">
  <p>{{item.name}}</p>
  <p>{{item.count}}</p>
  <button>increment</button> // увеличить item.count
  <button>decrement</button> // уменьшить item.count
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-03-25
@morto

updateCount(item, change) {
  item.count += change;
}

<button (click)="updateCount(item,  1)">+</button>
<button (click)="updateCount(item, -1)">-</button>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question