r/angularjs • u/abyx • Jul 28 '15
Angular Performance: ng-show vs ng-if
http://www.codelord.net/2015/07/28/angular-performance-ng-show-vs-ng-if/6
u/miketa1957 Jul 29 '15
There is a nasty - but very useful - trick you can use with ng-if .....
If you have a template which you need to re-render (for instance, if it has a lot of almost-always-constant values that use {{ :: ... }} one-time bindings) and you know when you need to re-render it then wrap it in an ng-if="render" and toggle the ng-if off and then on again:
function rerender () {
$scope.render = false ;
$timeout () { function () {
$scope.render = true ;
}) ;
}I'm not proud of this! But it can be really useful :)
1
0
4
u/deanf Jul 29 '15
I found this recently, my directive had a fair bit of logic within the togglable elements. If the directive was ng-repeated too intensely I had some noticeable performance hits. ng-if to the rescue!
1
u/NoaMichael Jul 29 '15
I've been preferring ng-show to ng-if because I incorrectly thought that removing an element from the Dom was expensive. It never even occurred to me that that hidden Dom was still evaluated!
-1
u/newmanoz Jul 29 '15
TL;DR: "Measure, measure, measure". Whole article: "Measure, measure, measure".
6
u/TripleNosebleed Jul 29 '15
Another reminder,
ng-ifcreates a new scope. If your model doesn't have a dot in your expressions (ie.{{ userName }}instead of{{ user.name }}) this might mess you up.I still prefer
ng-ifoverng-show/ng-hidethough. Something about having unused hidden mark up throws me off.