I want to use multiple classes inside Angular [ngClass]. I have two classes, It should work accordingly as per the condition of the flag, that are already passed from the component.ts.
Asked
Active
Viewed 7.4k times
25
-
4Welcome to stackoverflow! You'll need to share some more information along with some code. Please have a look around and read through the [help center](https://stackoverflow.com/help). In particular [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Nicholas K Jul 25 '19 at 07:14
5 Answers
60
Do like this:
<div [ngClass]="condition ? 'class1' : 'class2' " ></div>
(Ternary Operator Usage)

Tushar
- 1,948
- 1
- 13
- 32
32
You can do this in several ways :
number one :
[class.my-class]="step=='step1'"
number twe :
[ngClass]="{'my-class': step=='step1'}"
number three :
[ngClass]="{'my-class': step=='step1', 'my-class2':step=='step2' }"
number four :
[ngClass]="(step=='step1')?'my-class1':'my-class2'"
you can get help this link for more help

nima amr
- 603
- 1
- 6
- 13
5
You can try [ngClass]="condition ? 'checked' : 'unchecked'"
or [ngClass]="[condition ? 'checked' : 'unchecked']"

Seba Cherian
- 1,755
- 6
- 18
2
html :
<div [ngClass]="{'class1' : value == 1, 'class2' : value == 2}">
.......................
</div>
by using a function
<div [ngClass]="getClass(2)">
.......................
</div>
ts :
export class AppComponent {
value = 1;
getClass(value){
if(value == 1) return 'class1'
else if(value == 2) return 'class2'
}
}

Bhanu Tej P
- 220
- 1
- 5