AngularJs alternative to ng-if to save digest

爱⌒轻易说出口 提交于 2020-01-13 19:36:33

问题


I recently started studying about digest and performance improvements in AngulaJs and found on my website that I'm using tons of ng-if.

Sometimes in ng-if there is a variable that may change, but often is fixed at the startup of the controller and then never changes.

What should I do so to improve performance avoiding digest to evaluate every loop those unchangeable ng-if? Should I change directive? With what?

E.g

In my header template I have a div that can be seen only by particular type of user. It's just a div, so I don't want to call some different template. I put <div ng-if="userIsSuperior()"> ... </div>

When first evaluated, the return vale of userIsSuperior() never changes (during this session of course), but I know that AngularJs Digest evaluates it every loop. How can I avoid this? Or am I missing something?


回答1:


I think what you are looking for is one-time binding.

If you use:

<div ng-if="::userIsSuperior()"> ... </div>

Then the value of userIsSuperior() will only be calculated once and will stick to that value.

See demo.




回答2:


First answer solutions needs AngularJS > 2.

I found a valid solutions in using OnceJS, library for one-time-binding.

Here



来源:https://stackoverflow.com/questions/37559451/angularjs-alternative-to-ng-if-to-save-digest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!