How to use ngclass
?
<div [ngClass]="{
class0: condition,
classN: condition
}"></div>
And how would class-binding looks like?
<div [class.class0]="condition"
[class.classN]="condition"
></div>
app.component.class.html => contains class-binding version of the template
app.component.ngclass.html => contains ngclass version of the template
app.component.css => basic css to provide the demo styles
app.component.ts => align state and callbacks for the buttons to change the alignment
index.html => basic resetted index file
just clone the repository and execute npm start
.
on http://localhost:4200 you can direct interact with the current selected version.
- Open the Devtools
- Open the angular profiler plugin
- switch to "profiler" tab
- start recording by click the round button which become red by hover
- press
- top
- bottom
- left
- right
- center
- click "safe profile" to export a json with the detail report
Direction | ngclass | Class-binding |
---|---|---|
top | 0,4999 | 0,2999 |
bottom | 0,7000 | 0,4000 |
left | 0,6000 | 0,4000 |
right | 0,3999 | 0,3000 |
Center | 0,4000 | 0,0999 |
to avoid complexity in your change detection circle you should probably use class-binding instead of ngclass.
for more complex usecases or if you want to bind a object with the typesignature Record<string, boolean>
ngclass would be the right way.