Skip to content

ngClass causes unnecessary CSS class mutation even if computed classes are unchanged #25518

@pkozlowski-opensource

Description

@pkozlowski-opensource

I'm submitting a...


[x] Bug report  

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-jxxkvh?file=src%2Fapp%2Fapp.component.ts

Steps to reproduce:

  • open DevTools "Elements" tab and inspect the <div> with the I'm red content
  • click on the I'm just triggering CD button and observe that the red CSS class gets removed and added just after.

Current behavior

CSS class is removed and added just after, even if a set of CSS classes doesn't change based on the model provided.

Expected behavior

There are no unnecessary CSS class mutations.

What is the motivation / use case for changing the behavior?

Current behaviour causes unnecessary DOM access impacting overall performance.
It can also tip off people using mutation observers as reported in ng-bootstrap/ng-bootstrap#2582 (comment)

Environment

Angular version: ALL
Browser: ALL

Additional info

This happens because I'm giving ngClass a new object on each CD. NgClass / differs assume that a new object might have different shape and removes classes from the old object. We should probably be smarter about it...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions