A similat UseCase: iterate through collection and apply a specific class item-updated
for items
in collection
which were edited.
This part is optional, just for demo: [attr.title]="item.isDirty ? 'Item was updated' : null"
html component
<div *ngFor="let item of collection">
<label>Name: {{item.name}}</label>
<button class="btn"
[ngClass]="{'item-updated' : item.isDirty}"
[attr.title]="item.isDirty ? 'Item was updated' : null">
Save
</button>
</div>
Explanations:
Only when the item.isDirty
condition is matched, the button will receive the specific class.
In your case, from question, all your buttons are binded to the same isClicked
property. So, does not matter which one is edited, your property changes, and all other buttons receive the newest class.
EDIT:
The code snippet from question:
<button [ngClass]="{'newstyle': isClicked }" (click)=" isClicked = !isClicked">Click button</button>
Transformed
HTML component
<div *ngFor="let item of yourArray">
<label *ngIf="item.isClicked">Item was Clicked: {{item.id}}</label>
<button [ngClass]="{'newStyle' : !item.isClicked}" (click)="changeItemState(item)">Click button </button>
</div>
TS component
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'your-component',
templateUrl: './your.component.html'
})
export class YourComponent implements OnInit {
public yourArray: any[];
public ngOnInit(): void {
this.yourArray = [
{
id: 1,
name: 'First',
isClicked: false;
},
{
id: 2,
name: 'Second',
isClicked: false;
},
{
id: 3,
name: 'Third',
isClicked: true;
},
];
}
public changeItemState(item: any): void {
item.isClicked = !item.isClicked;
}
}