Skip to content

Document difference between [NgClass] and [class] #40623

Closed

Description

@jelbourn

Activity

  1. added
    P3
    on Jan 28, 2021
  2. modified the milestone: Backlog on Jan 28, 2021
  3. heinsc commented on Feb 2, 2021

    @heinsc

    Hi,
    ... I hope I'm right here ...
    Regarding to this, there seems to be something wrong in the documentation in "UNDERSTANDING ANGULAR" -> "Templates"-> "Attribute, class and style bindings". Under "Binding to multiple CSS classes" there is written:

    To bind to multiple classes, use [class] set to an expression—for example, [class]="classExpression". The expression can be one of:

    • [...]
    • An object with class names as the keys and truthy or falsy expressions as the values.

    I'm quite new to Angular, but I think that is only correct for [ngClass], but not for [class]. When I put in the "live example" at the appropriate place
    classExpression = //"special clearance";
    {special: true
    , clearance: true};
    it doesn't work.

    Am I correct with my suggestion, or am I incorrect with my coding?

  4. cesperian commented on Feb 22, 2021

    @cesperian
    Contributor

    @aikidave I don't see ngclass directive mentioned in the page this issue is referencing or in that page's recent history.

    There's definitely no mention of replacing an NgClass directive with a class directive. Maybe the original intent was just to add a (brief) section on NgClass in this page? Along with a link to the entry in the api, maybe?

    If you think that is the case than you can assign this to me, please...

  5. aikithoughts commented on Feb 22, 2021

    @aikithoughts

    @cesperian Let me dig into this one a bit more before I assign it out! Thanks!

  6. hannah23280 commented on Jul 18, 2021

    @hannah23280

    Hi,
    May i know what is the thing that [ngClass] can do, but [class] cannot do.
    Cos if [class] can do everything that [ngClass] can do, i will prefer to use [class].

  7. rafaelss95 commented on Sep 2, 2021

    @rafaelss95
    Contributor

    @jelbourn, it looks like the template-syntax link is out of date and I couldn't even find it at https://angular.io/guide/attribute-binding (should be there after #36954, which separated the page you mentioned into multiple).

    After a quick search in commits, I found that the statement about replacing ngClass/ngStyle with class/style was committed on #35066 (7526ef7), but specifically the ngClass -> class (oh, the ngStyle -> style is still there) was removed on #38860 (4dc8d83)... so is it still true that the team recommends the ngClass replacement?

  8. kannanwisen commented on Dec 17, 2022

    @kannanwisen

    In class attribute, the view can be changed only when the value object identity is changed.
    In ngClass Directive binding, the view can be changed when the value is changed.

  9. added
    area: core
    on Dec 17, 2022
  10. 8 remaining items

  11. added
    area: docs
    and removed on Mar 30, 2024
  12. FrancescoBorzi commented on Jan 27, 2025

    @FrancescoBorzi

    we suggest that people should start replacing [ngClass] bindings with [class] bindings

    is this still actual? where is it on the new angular.dev site?

  13. JeanMeche commented on Jan 27, 2025

    @JeanMeche
    Member

    We updated our docs with #59240

  14. LuizVelosoDTI commented on Feb 18, 2025

    @LuizVelosoDTI

    There is one more difference I observed...

    <h1 [class]="{ 'xyz addBg': true, addFont: true}">{{selectedColor}}</h1>
    

    This snippet is not able to add xyz addBg to the element's class it's only adding addFont . Producing following markup..
    <h1 _ngcontent-xwe-c16="" class="addFont">red</h1>
    in the other hand ngClass ...

    <h1 [ngClass]="{ 'xyz addBg': true, addFont: true}">{{selectedColor}}</h1>
    

    producing below result

    <h1 _ngcontent-yxm-c16="" ng-reflect-ng-class="[object Object]" class="xyz addBg addFont">red</h1>
    

    Good observation. I never even thought of combine multiple css classes in a single attribute (as in "xyz addBg", although I not sure if this is considered a good practice. given the flexibility of ngClass, seem that ngClass is better than class. On other hand, [class] less flexible, but ensure good practice (view is changed only when its object identity change, thus encourage immutability ,e g)

    In a project using tailwind, it is very common to use ngClass with space, when multiple classes apply to a single condition

  15. added 2 commits that reference this issue on Jul 12, 2025
    96f039b
    b011868
  16. added a commit that references this issue on Jul 14, 2025
    ebfc946
  17. added a commit that references this issue on Jul 14, 2025
    94e0a46
  18. angular-automatic-lock-bot commented on Aug 14, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  19. locked and limited conversation to collaborators on Aug 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: corearea: docscore: styling bindings

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions