Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS using ng-switch without wrapper div

Tags:

angularjs

I would like to use ng-switch because I do not want the other elements that I do not want to show to be part of the DOM. That is why i did not use ng-hide/ng-show. In the example below, I would like to only have the span tag be in the DOM without the div wrappers from the ng-switch. What is the best way to accomplish this?

<div ng-switch on="user">
    <div ng-switch-when="true">
        <span>One</span>
    </div>
    <div ng-switch-when="false">
        <span>Two</span>
    </div>
</div>
like image 324
teggy Avatar asked May 06 '13 20:05

teggy


3 Answers

You can use the ng-switch directive as a custom element and not specify the div in the first place. For example:

<ng-switch on="user">
  <span ng-switch-when="true">One</span>
  <span ng-switch-default>Two</span>
</ng-switch>

Here is a plunker to play around with: http://plnkr.co/edit/zni6raUWOguhQh9jDiY3

like image 171
Chris Auer Avatar answered Nov 12 '22 16:11

Chris Auer


the solution provided by @ChrisAuer this still creates a wrapping element. AFAIK you'd have to use a custome directive. You may want to use angular-ui if

<div ui-if="user">
    <span>One</span>
</div>
<div ui-if="!user">
    <span>Two</span>
</div>

Probably, in your case, you'd be fine using ng-show or ng-hide which only hide(display:none) the element - they don't remove it form the DOM.

<div ng-show="user"> <!-- same as ng-hide="!user" -->
    <span>One</span>
</div>
<div ng-hide="user"> <!-- same as ng-show="!user" -->
    <span>Two</span>
</div>
like image 1
g00fy Avatar answered Nov 12 '22 16:11

g00fy


I would say use ng-if, like this:

<div>
    <div ng-if="user==true">
        <span>One</span>
    </div>
    <div ng-if="user==false">
        <span>Two</span>
    </div>
</div>
like image 1
RedPoppy Avatar answered Nov 12 '22 17:11

RedPoppy