There are a lot of SO questions about the difference between ngClass and class like this one:
Difference between [ngClass] vs [class] binding
But you can also use [className] as a binding. What is the difference between [ngClass] and [className] and when should you use one over the other?
Like [className], [ngClass] allows to bind a string expression of space separated class names:
<some-element [ngClass]="'first second'">...</some-element>
<some-element [className]="'first second'">...</some-element>
But [ngClass] also allows to bind the class names as an array or as an object (where each property key is a class name which is applied or not according to the associated value):
<some-element [ngClass]="['first', 'second']">...</some-element>
<some-element [ngClass]="{'first': condition1, 'second': condition2}">...</some-element>
These syntaxes, which can be convenient, are not supported by [className] since className is a string property of HTML elements.
See this stackblitz for a demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With