I'm trying to understand the difference between ng-if
and ng-show
/ng-hide
, but they look the same to me.
Is there a difference that I should keep in mind choosing to use one or the other ?
I'm trying to understand the difference between ng-if
and ng-show
/ng-hide
, but they look the same to me.
Is there a difference that I should keep in mind choosing to use one or the other ?
The
ng-if
directive removes the content from the page andng-show/ng-hide
uses the CSSdisplay
property to hide content.This is useful in case you want to use
:first-child
and:last-child
pseudo selectors to style.ng-if if false will remove elements from DOM. This means that all your events, directives attached to those elements will be lost. For example, ng-click to one of child elements, when ng-if evaluates to false, that element will be removed from DOM and again when it is true it is recreated.
ng-show/ng-hide does not remove the elements from DOM. It uses CSS styles (.ng-hide) to hide/show elements .This way your events, directives that were attached to children will not be lost.
ng-if creates a child scope while ng-show/ng-hide does not.
@Gajus Kuizinas and @CodeHater are correct. Here i am just giving an example. While we are working with ng-if, if the assigned value is false then the whole html elements will be removed from DOM. and if assigned value is true, then the html elements will be visible on the DOM. And the scope will be different compared to the parent scope. But in case of ng-show, it wil just show and hide the elements based on the assigned value. But it always stays in the DOM. Only the visibility changes as per the assigned value.
http://plnkr.co/edit/3G0V9ivUzzc8kpLb1OQn?p=preview
Hope this example will help you in understanding the scopes. Try giving false values to ng-show and ng-if and check the DOM in console. Try entering the values in the input boxes and observe the difference.
Hello Plunker!
@EdSpencer is correct. If you have a lot of elements and you use ng-if to only instantiate the relevant ones, you are saving resources. @CodeHater is also somewhat correct, if you are going to remove and show an element very often, hiding it instead of removing it could improve performance.
The main use case I find for ng-if is that it allows me to cleanly validate and eliminte an element if the contents is illegal. For instance I could reference to a null image name variable and that will throw an error but if I ng-if and check if it's null, it's all good. If I did an ng-show, the error would still fire.
Fact, that
ng-if
directive, unlikeng-show
, creates its own scope, leads to interesting practical difference:At first list,
on-click
event,show
variable, from innner/own scope, is changed, butng-if
is watching on another variable from outer scope with same name, so solution not works. At case ofng-show
we have the only oneshow
variable, that is why it works. To fix first attempt, we should reference toshow
from parent/outer scope via$parent.show
.ngIf makes a manipulation on the DOM by removing or recreating the element.
Whereas ngShow applies a css rules to hide/show things.
For most of the cases (not always), I would summarize this as, if you need a one time check to show/hide things, use
ng-if
, if you need to show/hide things based on the user actions on the screen (like checked a checkbox then show textbox, unchecked then hide textbox etc..), then useng-show