Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Click + ngClass, how to apply ONLY to SELF but not ALL elements inside ngFor

In Angular 1.x this following code works as I want to to click and flip a card inside an ng-repeat

<div class="card" ng-repeat="let card of cards">
<div class="flipcard" ng-class="{'flipped':isflipped}" ng-click="isflipped = !isflipped">
</div>
</div>

However in Angular 2 when clicked, it flipped every "card" inside the ngFor loop... how do I bind the ngClass condition to the element itself only?

<div class="card" *ngFor="let card of cards">
<div class="flipcard"  [ngClass]="{'flipped': isflipped }" (click)="isflipped = !isflipped;">
</div>
</div>
like image 672
adrian li Avatar asked Feb 07 '23 12:02

adrian li


1 Answers

Change it to:

<div class="card" *ngFor="let card of cards">
    <div class="flipcard"  [ngClass]="{'flipped': card.isflipped }" (click)="card.isflipped = !card.isflipped;">
    </div>
</div>
like image 132
Harry Ninh Avatar answered Feb 13 '23 06:02

Harry Ninh