在ng-if/* ngIf中使用函数调用是一种不好的做法吗? [英] Is it a bad practice to use function call in ng-if/*ngIf?

查看:252
本文介绍了在ng-if/* ngIf中使用函数调用是一种不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ng-if内部使用函数调用的返回值是一种不好的做法.它会影响摘要周期/性能吗?

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

推荐答案

与视图中使用的任何表达式一样,将在每个摘要周期重新评估它的值,以查看其值是否已更改,因此是否必须更改元素.删除或添加到DOM.

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.

如果功能足够简单,那就没问题.如果它尝试计算pi的第10000个小数,则会出现性能问题.重要的是要了解自己在做什么以及它是如何工作的.

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.

这篇关于在ng-if/* ngIf中使用函数调用是一种不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆