90

How to use multiple conditions for ngClass? Example:

<section [ngClass]="[menu1 ? 'class1' : '' || menu2 ? 'class1' : '' || (something && (menu1 || menu2)) ? 'class2' : '']">

something like this. I got same class for 2 menus, and I need class when one of those menus is true and 'something' is true. Hope I explained well enough

Nemanja G
  • 1,760
  • 6
  • 29
  • 48

10 Answers10

163

You are trying to assign an array to ngClass, but the syntax for the array elements is wrong since you separate them with a || instead of a ,.

Try this:

<section [ngClass]="[menu1 ? 'class1' : '',  menu2 ? 'class1' : '', (something && (menu1 || menu2)) ? 'class2' : '']">

This other option should also work:

<section [ngClass.class1]="menu1 || menu2" [ngClass.class2] = "(menu1 || menu2) && something">    
DJ22T
  • 1,628
  • 3
  • 34
  • 66
Matthias247
  • 9,836
  • 1
  • 20
  • 29
82

you need object notation

<section [ngClass]="{'class1':condition1, 'class2': condition2, 'class3':condition3}" > 

ref: NgClass

Kondal
  • 2,870
  • 5
  • 26
  • 40
dee zg
  • 13,793
  • 10
  • 42
  • 82
20
<section [ngClass]="{'class1': expression1, 'class2': expression2, 'class3': expression3}">

Don't forget to add single quotes around class names.

Gourav Pokharkar
  • 1,568
  • 1
  • 11
  • 33
17

There are multiple ways to write same logic. As it mentioned earlier, you can use object notation or simply expression. However, I think you should not that much logic in HTML. Hard to test and find issue. You can use a getter function to assign the class.

For Instance;

public getCustomCss() {
    //Logic here;
    if(this.x == this.y){
        return 'class1'
    }
    if(this.x == this.z){
        return 'class2'
    }
}
<!-- HTML -->
<div [ngClass]="getCustomCss()"> Some prop</div>

//OR

get customCss() {
    //Logic here;
    if(this.x == this.y){
        return 'class1'
    }
    if(this.x == this.z){
        return 'class2'
    }
}
<!-- HTML -->
<div [ngClass]="customCss"> Some prop</div>
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
  • never use getters inside the template, if there are props that are passed to this "child" component from its parent, angular will execute the getter each time to check if the data was changed. – Mehdi Fracso Jun 02 '20 at 07:51
  • that is wat you want.. isn't it ?? whenever data change, check for condition ... – xdeepakv Jun 02 '20 at 19:29
  • how about passing a param in the func ? Lets say that method is in a *ngfor – Gel Oct 18 '21 at 15:16
12
<a [ngClass]="{'class1':array.status === 'active','class2':array.status === 'idle','class3':array.status === 'inactive',}">
8

you can use specific class instead of use [ngClass] .Please note that in the second example I have used the age variable as an expiation purpose .

Example 1:

<section
    [class.class1]="menu1"
    [class.class2]="menu2"
    [class.class3]="menu3">
    </section>

Example 2:

   <section
    [class.class1]="age > 20"
    [class.class2]="age < 50"
    [class.class3]="age == 4">
    </section>
Hoque MD Zahidul
  • 10,560
  • 2
  • 37
  • 40
2

I hope this is one of the basic conditional classes

Solution: 1

<section [ngClass]="(condition)? 'class1 class2 ... classN' : 'another class1 ... classN' ">

Solution 2

<section [ngClass]="(condition)? 'class1 class2 ... classN' : '(condition)? 'class1 class2 ... classN':'another class' ">

Solution 3

<section [ngClass]="'myclass': condition, 'className2': condition2">
Md. Abu Sayed
  • 2,396
  • 2
  • 18
  • 26
1

I had this similar issue. I wanted to set a class after looking at multiple expressions. ngClass can evaluate a method inside the component code and tell you what to do.

So inside an *ngFor:

<div [ngClass]="{'shrink': shouldShrink(a.category1, a.category2), 'showAll': section == 'allwork' }">{{a.listing}}</div>

And inside the component:

section = 'allwork';

shouldShrink(cat1, cat2) {
    return this.section === cat1 || this.section === cat2 ? false : true;
}

Here I need to calculate if i should shrink a div based on if a 2 different categories have matched what the selected category is. And it works. So from there you can computer a true/false for the [ngClass] based on what your method returns given the inputs.

iowayankee
  • 11
  • 2
1

A more simple way to add multiple dynamic classes, you can simply use:

<div [ngClass]="[class1, class2, condition ? class3 : '']"> Some content</div>
Shadoweb
  • 5,812
  • 1
  • 42
  • 55
0

For a more sophisticated situations, this can be helpful

<div [ngClass]="{'Class1':(condition1 || condition2), 'class2':( (condition1 || condition2) && condition3 )}"> Some content</div>
Ali
  • 1,633
  • 7
  • 35
  • 58