Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad practice to use function call in ng-if/*ngIf? [duplicate]

Tags:

angularjs

Is it a bad practice to use the return value of a function call inside ng-if. Will it affect the digest cycle/performance?

like image 526
Saurabh Maurya Avatar asked Sep 01 '25 10:09

Saurabh Maurya


1 Answers

As any expression used in the view, it will be reevaluated at each digest cycle to see if its value has changed or not, and thus if the element must be removed or added to the DOM.

If the function is simple enough, no problem. If it tries to compute the 10000th decimal of pi, you'll have performance problems. What matters is to understand what you're doing, and how it works.

So if you feel using a function makes your code simpler, go for it, and start optimizing if you really need to.

like image 105
JB Nizet Avatar answered Sep 04 '25 22:09

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!