You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document difference between [NgClass] and [class] #40623
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimearea: docsRelated to the documentationcore: styling bindings
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?
@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...
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].
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?
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.
Reacted by Trần Đức Phương, Arturo Balbuena and Andrew Spoda
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 ...
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
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimearea: docsRelated to the documentationcore: styling bindings
Activity
heinsc commentedon Feb 2, 2021
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:
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?
cesperian commentedon Feb 22, 2021
@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...
aikithoughts commentedon Feb 22, 2021
@cesperian Let me dig into this one a bit more before I assign it out! Thanks!
hannah23280 commentedon Jul 18, 2021
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].
rafaelss95 commentedon Sep 2, 2021
@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/ngStylewithclass/stylewas committed on #35066 (7526ef7), but specifically thengClass->class(oh, thengStyle->styleis still there) was removed on #38860 (4dc8d83)... so is it still true that the team recommends thengClassreplacement?kannanwisen commentedon Dec 17, 2022
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.
8 remaining items
FrancescoBorzi commentedon Jan 27, 2025
is this still actual? where is it on the new angular.dev site?
JeanMeche commentedon Jan 27, 2025
We updated our docs with #59240
LuizVelosoDTI commentedon Feb 18, 2025
In a project using tailwind, it is very common to use ngClass with space, when multiple classes apply to a single condition
docs: add note about difference between class bindings and `ngClass`
docs: add note about difference between class bindings and `ngClass`
docs: add note about difference between class bindings and `ngClass` (#…
docs: add note about difference between class bindings and `ngClass` (a…
angular-automatic-lock-bot commentedon Aug 14, 2025
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.