V. Pivet
V. Pivet

Reputation: 1328

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 ? :)

Upvotes: 5

Views: 17947

Answers (3)

Suraj Rao
Suraj Rao

Reputation: 29635

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>

Upvotes: 15

Meshal Alhazmi
Meshal Alhazmi

Reputation: 356

try this

<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">

Upvotes: 3

AishApp
AishApp

Reputation: 4162

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>

Upvotes: 9

Related Questions