Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use if-else condition with [ngClass] in angular 7? [duplicate]

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.

like image 702
Akshay Kumar Avatar asked Jul 25 '19 07:07

Akshay Kumar


People also ask

How do you write if else condition in NgClass?

You can do like this [ngClass]="{'class1':condition1, 'class2':condition2}" .

Can we have two NgClass?

Yes it does, I just had to use ! important in css to make it visible.

Can we use NgStyle and NgClass together?

Combining NgStyle and NgClass Directives with our knowledge of Angular Template Syntax, we can marry conditional styling with Angular's Property & Event Binding.


Video Answer


1 Answers

Do like this:

<div [ngClass]="condition ? 'class1' : 'class2' " ></div>

(Ternary Operator Usage)

like image 79
Tushar Avatar answered Sep 21 '22 13:09

Tushar