92

What is the difference in Angular 2 between the following snippets:

<div [class.extra-sparkle]="isDelightful">
<div [ngClass]="{'extra-sparkle': isDelightful}">
luiscla27
  • 4,956
  • 37
  • 49
Ben Taliadoros
  • 7,003
  • 15
  • 60
  • 97

8 Answers8

81

This is special Angular binding syntax

<div [class.extra-sparkle]="isDelightful">

This is part of the Angular compiler and you can't build a custom binding variant following this binding style. The only supported are [class.xxx]="...", [style.xxx]="...", and [attr.xxx]="..."

ngClass is a normal Angular directive like you can build it yourself

<div [ngClass]="{'extra-sparkle': isDelightful}">

ngClass is more powerful. It allows you to bind a string of classes, an array of strings, or an object like in your example.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    Ah so in the docs when it says
    it's binding the role attribute of the div not the role attribute of an object called attr from the controller. Makes sense, thanks
    – Ben Taliadoros Jul 26 '17 at 08:37
  • 3
    `attr.foo` is for binding to attributes instead of properties. See https://stackoverflow.com/questions/6003819/properties-and-attributes-in-html. `@Input()` of Angular components and directives are treated as properties. Binding to attributes only supports string values, attributes support any kind of value. If there is an `@Input()` with a matching name used to the bind to the attribute, the value will also be passed to the `@Input()`. If you bind to an elements native attribute (like `for` in ` – Günter Zöchbauer Jul 26 '17 at 08:41
  • 1
    Thanks, although I think "Binding to attributes only supports string values, attributes support any kind of value." may have been mis-typed? – Ben Taliadoros Jul 26 '17 at 09:01
  • 1
    @BenTaliadoros thanks, you're right. It should be "**properties** support any kind of value". – Günter Zöchbauer Jul 26 '17 at 09:02
  • 1
    So, which one is better in this scenario for performance? Doesn't matter which one you use? – Esteban Chornet Nov 25 '19 at 11:23
  • Binding to `class` is probably faster, because it just assigns the whole string. `ngClass` has to watch the expression and might have to add/remove individual classes when the expression changes. It's usually better to use `ngClass` anyway, because binding to `class` just overwrites classes that might be set from elsewhere and the performance argument is probably just a premature optimization case. – Günter Zöchbauer Nov 25 '19 at 12:22
  • well, in fact [class]="{'extra-sparkle': isDelightful}" is also working... – romainm Apr 28 '20 at 08:37
  • 1
    @romanm Yes, that should work as well. The difference is that your version will override classes added by other means, because `[class]` is the full HTML `class="xxx"` property/attribute, while the variants in my answer add/remove only the class `extra-sparkle` and leave other classes as is. – Günter Zöchbauer Apr 28 '20 at 08:50
32

The above two lines of code is with respect to CSS class binding in Angular. There are basically 2-3 ways you can bind css class to angular components.

You provide a class name with class.className between brackets in your templates and then an expression on the right that should evaluate to true or false to determine if the class should be applied. That is the below one where extra-sparkle(key) is the css class and isDelightful(value).

<div [class.extra-sparkle]="isDelightful">

When multiple classes should potentially be added, the NgClass directive comes in really handy. NgClass should receive an object with class names as keys and expressions that evaluate to true or false. extra-sparkle is the key and isDelightful is the value (boolean).

<div [ngClass]="{'extra-sparkle': isDelightful}">

Now along with extra sparkle, you can glitter your div also.

<div [ngClass]="{'extra-sparkle': isDelightful,'extra-glitter':isGlitter}">

or

export class AppComponent {
    isDelightful: boolean = true;
    isGlitter:  boolean = true;
    
    get sparkleGlitter()
    {
        let classes = {
            extra-sparkle: this.isDelightful,
            extra-glitter: this.isGlitter
        };
        return classes;
    }
}

<div [ngClass]='sparkleGlitter'>

For ngClass, you can have conditional ternary operators too.

luiscla27
  • 4,956
  • 37
  • 49
Hameed Syed
  • 3,939
  • 2
  • 21
  • 31
19

In the current version of Angular following syntax is also working fine:

[class]="{toggled: sidebarActive, test: true,test1: sidebarActive}

So to my understanding, there is no difference between using ngClass and [class] binding?

Example

DuDa
  • 3,718
  • 4
  • 16
  • 36
  • Are you asking a question? – Scratte Nov 30 '20 at 23:07
  • 4
    I'm answering the question but it's pretty old. The only difference I was able to find was that ngClass can handle multiple class binding whilst [class] binding can handle only one but that seems this is not true. So my only question is - is there any difference? – Daniel Budzyński Nov 30 '20 at 23:20
  • 1
    You're not suppose to ask a question in the Answer box :) See the [tour](https://stackoverflow.com/tour) and [how to answer](https://stackoverflow.com/help/how-to-answer). But you're saying that half of your post is an answer.. and the other is a question. – Scratte Nov 30 '20 at 23:28
  • 10
    the great part of my answer is actually answering the question in regards to the new version of angular. So as I said before, I'm not asking the question, the "question" I've asked is rather rhetorical and axillary. – Daniel Budzyński Nov 30 '20 at 23:59
15

Using [ngClass] you're able to apply multiple classes in a really convenient way. You can even apply a function that will return an object of classes. [class. makes you able to apply only one class (of course you can use class. a few times but it looks really bad).

elzoy
  • 5,237
  • 11
  • 40
  • 65
8

In [ngClass] you can add one of two different classes like this:

<div [ngClass]="a === b ? 'class1' : 'class2'">
luiscla27
  • 4,956
  • 37
  • 49
FierceDev
  • 685
  • 6
  • 13
5

After going through multiple answers, I thought to add my view as this might be helpful.

So, Angular is planning to remove [ngClass] and [ngStyle] directives in their future releases.

As of now (Angular-v13), [class] binding supports to add multiple css classes. Though there are some feature gaps when compared with [ngClass]. You can check this - https://github.com/angular/angular/issues/40623

[ngClass]

<p [ngClass] = “‘addGreen’”> Hello World </p> 
<p [ngClass] = “[‘addGreen’, ‘addFont’, ‘addBg’]”> Hello World </p>
<p [ngClass] = “{ addGreen: true, addBg: true, addFont: false }”> Hello World </p> 
<p [ngClass] = “{ addGreen addBg: true, addFont: false}”> Hello World </p>

[class]

<p [class] = “‘addGreen’”> Hello World </p> 
<p [class] = “[‘addGreen’, ‘addFont’, ‘addBg’]”> Hello World </p>
<p [class] = “{ addGreen: true, addBg: true, addFont: false }”> Hello World </p> 

Note : The below way of adding the multiple classes is not possible

<p [class] = “{ addGreen addBg: true, addFont: false}”> Hello World

Shivaay
  • 359
  • 1
  • 6
  • 19
0

Yes, with help of class binding ([class]) also we can attach multiple css classes with latest versions. ex:[class]='{object with key as class name and value as true/false}' same like [ngClass] so..there is no difference between [class] binding and inbuilt [ngClass] directive

0

No one has mentioned that if you use both [class] and [ngClass] bindings some confusion can occur once you try to pass some class string to [class] that is 'registered' by [ngClass]. They can compete.

E.g. this will not assign the 'd-flex' class value to the [class] binding as long as an [ngClass] condition which is associated with this class has evaluates to true:

component.html

<div [ngClass]="{'d-flex': falseCondition}" [class]="getClassBasedOnCondition()">

component.ts

public getClassBasedOnCondition(): string {
    return trueCondition ? 'd-flex' : '';
}

You can use [class.] instead of [ngClass] to avoid such behavior, as long as you don't have multiple classnames there, such as [ngClass]="{'d-flex p-1': condition}"

Michahell
  • 4,905
  • 5
  • 29
  • 45
Vlad
  • 985
  • 1
  • 6
  • 10
  • To add to this answer, one can consider styling precedences from https://angular.io/guide/style-precedence#style-precedence. Because ngClass (and the same should apply to ngStyle) is a directive, and atribute bindings have precedence over stylings made by directives, [class]="classExpr" or [class.klass]="expr" should override ngClass. – matias Jun 06 '22 at 21:53