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 3.9k times
15
-
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
38
Do like this:
<div [ngClass]="condition ? 'class1' : 'class2' " ></div>
(Ternary Operator Usage)
Tushar
- 1,602
- 1
- 15
- 29
22
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
- 443
- 1
- 4
- 13
4
You can try [ngClass]="condition ? 'checked' : 'unchecked'" or [ngClass]="[condition ? 'checked' : 'unchecked']"
Seba Cherian
- 1,720
- 4
- 18
1
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
- 200
- 5