0

I need to toggle an element that needs to be initially visible. In my case the toggle works fine but the element (menu class) is initially hidden...

JSFIDDLE

Here is my code:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
    <a href="#" class="toggle-menu" ng-click="collapsed=!collapsed">Click to toggle menu</a>
  <div class="menu" ng-show="collapsed">
    <ol>
      <li>Item</li>
      <li>Item</li>
      <li>Item</li>
    </ol>
  </div>
</div>
Khaled Awad
  • 1,644
  • 11
  • 24
Valip
  • 4,440
  • 19
  • 79
  • 150

3 Answers3

1

If not initialized, the default value of collapsed is false. You can initialize scope.collapsed = true in your controller or directive instead of using ng-init because ng-init is costly.

Community
  • 1
  • 1
6324
  • 4,678
  • 8
  • 34
  • 63
0

instead of

<div class="menu" ng-show="collapsed">

use this line:

<div class="menu" ng-show="!collapsed">

fiddle

asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
0

You can use ng-init="expression" or set in a controller, but I prefer ng-init, since it propagates.

Okdan
  • 48
  • 1
  • 8