Angular 2单击+ ngClass,如何仅应用于SELF而不是ALLF中的所有元素 [英] Angular 2 Click + ngClass, how to apply ONLY to SELF but not ALL elements inside ngFor

查看:91
本文介绍了Angular 2单击+ ngClass,如何仅应用于SELF而不是ALLF中的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 1.x
中,以下代码可用,因为我想在ng-repeat中点击并翻转一张卡

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>

但是在Angular 2
点击时,它会翻转ngFor循环中的每个卡 ...如何仅将ngClass条件绑定到元素本身?

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>


推荐答案

将其更改为:

<div class="card" *ngFor="let card of cards">
    <div class="flipcard"  [ngClass]="{'flipped': card.isflipped }" (click)="card.isflipped = !card.isflipped;">
    </div>
</div>

这篇关于Angular 2单击+ ngClass,如何仅应用于SELF而不是ALLF中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆