Performance: ngStyle for dynamic styles
MEDIUM IMPACT
ngStyle affects how styles are applied dynamically, impacting rendering speed and layout recalculations when styles change.
<div [style.width.px]="dynamicWidth" [style.height.px]="dynamicHeight" [style.margin.px]="dynamicMargin"></div>
<div [ngStyle]="{ 'width.px': dynamicWidth, 'height.px': dynamicHeight, 'margin.px': dynamicMargin }"></div>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| ngStyle with many dynamic properties | Multiple style updates per change detection | Multiple reflows if layout styles change | High paint cost if many elements affected | [X] Bad |
| Individual style bindings for dynamic styles | Selective style updates | Reflows only when necessary | Lower paint cost | [OK] Good |