Change classes conditionally Ionic2

2019-01-28 01:56发布

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

3条回答
▲ chillily
2楼-- · 2019-01-28 02:15

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">
查看更多
▲ chillily
3楼-- · 2019-01-28 02:24

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>
查看更多
你好瞎i
4楼-- · 2019-01-28 02:37

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>
查看更多
登录 后发表回答