ngIf and ngSwitch AngularJS
Asked Answered
G

4

41

What's practical difference between ngIf and ngSwitch? Both directives manipulate the DOM, but ngSwitch is more verbose. Is the typical case to just use ngIf unless you need something really big in which case use ngSwitch?

Is there a situation where ngSwitch and ngIf are not direct replacements? Or is their only practical difference the syntax?

Garibay answered 24/5, 2013 at 19:3 Comment(0)
S
22

Michele Tilley's got it exactly right, I believe, particularly in pointing out the contrast with ngShow/ngHide. There's one additional difference to note: ng-If will detach and re-attach an element in-place. But ng-Switch has an outer containing element on which you declare the main directive and its condition: ng-switch="expression". The conditional content within that outer element will be append()-ed as the last child of the outer element, thus changing its position relative to any non-conditional content within the outer element.

And, see this CodePen for an interactive demo of all three, showing the differences in execution.


EDIT: This behavior changed in Angular 1.2. Elements are now left in place. The Codepen above mentions and demonstrates this, providing a link to a 1.08 Plunk that has sadly been wiped out...

Staple answered 2/11, 2013 at 23:35 Comment(0)
R
41

ngIf is basically a version of ngSwitch with a single condition. It's different from ngShow in that it removes the actual DOM element rather than simply hiding it. If you're using an ngSwitch with just a singly truthy condition check, then I believe ngIf will do the same thing.

Ropeway answered 24/5, 2013 at 19:45 Comment(0)
S
22

Michele Tilley's got it exactly right, I believe, particularly in pointing out the contrast with ngShow/ngHide. There's one additional difference to note: ng-If will detach and re-attach an element in-place. But ng-Switch has an outer containing element on which you declare the main directive and its condition: ng-switch="expression". The conditional content within that outer element will be append()-ed as the last child of the outer element, thus changing its position relative to any non-conditional content within the outer element.

And, see this CodePen for an interactive demo of all three, showing the differences in execution.


EDIT: This behavior changed in Angular 1.2. Elements are now left in place. The Codepen above mentions and demonstrates this, providing a link to a 1.08 Plunk that has sadly been wiped out...

Staple answered 2/11, 2013 at 23:35 Comment(0)
B
7

One more difference would be that ngIf and ngSwitch create new scopes, while ngShow/ngHide don't.

Boulevardier answered 2/4, 2015 at 23:29 Comment(1)
This is a useful comment, but in no way answers the actual question.Ultimogeniture
U
1

You can think in ngIf/ngSwitch in the same way you do with if/switch when you are coding. Obviously they do almost the same thing, but there are cases that a ngIf is better and there are cases where nfSwitch is better.

Underside answered 15/2, 2016 at 11:4 Comment(1)
For instance, ngSwitch is better if you have a few cases and a default case that is essentially != any of the aboveJarry

© 2022 - 2024 — McMap. All rights reserved.