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>

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

instead of

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

use this line:

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

fiddle

1 Comment

what is deference with OP post?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.