I've got a button element in an *ngFor array in my Angular2 app.
The structure of the button is:
<button>
<span class="price">{{ product.price }}</span>
<span class="text"> Add me! </span>
</button>
When I click on it I need to remove the text inside the span .text and put smth else inside, say <span class="text"> Added! </span> not display the .price span interpolation.
I tried to make it in the following way:
<button [textContent]="isPressed? 'Added!' : 'Add me' "></button>
And declare the corresponding property in the Class like:
private isPressed = false;
It works fine, but the problem is that it changes all buttons simultaneously, and I need to change only the clicked one.
What is the correct way to make it working properly?
isPresseduse a semantic variable likeproduct.isAdded. This way the state of the button is bound to the product in question.product, which refers to a specific object the button relates to. There's many products (that's why you need the loop) and therefore you have the state saved per product. Each button shows the state of one particular product. If you were using a generic variable likeisPressedthere is only one and all buttons are watching it. The answers below show you how to adjust your code. Upvote them :-)