简体   繁体   中英

Change classes conditionally Ionic2

I want to change my classe for an element but I don't know how to process with Ionic 2. I tried this way but it's wrong :

<span [ngClass]="{(user.id == this.session.userDefaultId) : 'class1 class2', (user.id != this.session.userDefaultId) : 'class3 class4'}">
...
</span>

Someone can explain me how to do somethink like that please ? :)

You can use a simple ternary operator to set the class for your example.

<span [ngClass]="(user.id == this.session.userDefaultId) ? 'class1 class2' : 'class3 class4'">
...
</span>

You have to mention the class name first followed by the condition

<span [ngClass]="{'class1 class2' : (user.id == this.session.userDefaultId), 'class3 class4':(user.id != this.session.userDefaultId)}">
...
</span>

试试这个

<span [class.class1]="user.id == this.session.userDefaultId" [class.class2]="user.id == this.session.userDefaultId" [class.class3]="user.id != this.session.userDefaultId" [class.class4]="user.id != this.session.userDefaultId">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM