1

Working in an Angular project I realized that when I used a method as the expression to be evaluated in a *ngIf directive, the method was called multiple times. Looking into the reason I found that it's normal and expected behavior, it will call on every change detection cycle:

The question then is: what type of expression should be used in this and others directives? A component variable (undefined|defined|true|false|...), a method returning a boolean, it depends on the implementation?

Does anyone know of any blog or best practices article where this topic is discussed and what they recommend?

Thanks.

Alavaros
  • 1,665
  • 7
  • 32
  • 52
  • The only best practice here is to avoid a computation heavy expression, anything else is fine. My personal favorite is to use only component variable but that's an opinion. – Kamalen Jun 14 '19 at 09:25
  • 1
    On Push Change Detection Strategy may help you to limitate such behavior, you can read more here : https://netbasal.com/a-comprehensive-guide-to-angular-onpush-change-detection-strategy-5bac493074a4 – Gérôme Grignon Jun 14 '19 at 09:42

0 Answers0