1

I am trying to toggle a class on click, please find my below code.

<li class="dropdown" data-ng-class="sign-open">
  <a href="" class="dropdown-toggle" data-ng-click="signToogle()">Sign In <b class="caret"></b></a>
  <div class="dropdown-menu" style="padding: 15px;">
    <form action="#" method="post" accept-charset="UTF-8" class="form-menu">
      <input id="user_username" type="text" name="user[username]" size="33" placeholder="Username">
      <input id="user_password" type="password" name="user[password]" size="33" placeholder="Password">
      <label class="checkbox muted hidden-tablet">
        <input type="checkbox">Remember Me</label>
      <input class="btn span3" type="submit" name="commit" value="Sign In">
    </form>

  </div>
</li>


//sign in show-hide
$scope.signToogle = function () {
    if ($scope.sign-open === "")
        $scope.class = "open";
    else
        $scope.class = "";
}

this js funciton will addclass open so if ul has open class as it's parent then it will be visible. But don't know how can I make that if click once then true and class append and if again clicked statement false and class will be removed.

1
  • you can check using the condition of class. like this `if($scope.class ==='open'){ //remove class....}' Commented Apr 23, 2015 at 11:41

2 Answers 2

5

You can use ng-class

<div ng-class="{active: is_active}">Some div</div>

<button ng-click="is_active = !is_active" ng-init="is_active=false">Click to toggle</button>

Set or reset $scope.is_active when you click

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

1 Comment

what about js function side code, can you please help in it as well. and will it add class active, if i want to add class open should i change it to "{open: is_active}"
2

var app = angular.module('app', []);

app.controller('Main', function($scope) {
  $scope.isOpen = false;
});
.item span {
  display: none;
}
.item.close span.show-on-close {
  display: block;
}
.item.open span.show-on-open {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">

  <div ng-controller="Main">
    <pre>isOpen: {{isOpen | json}}</pre>
    <span class="item" ng-class="{'open':isOpen, 'close':!isOpen}">
      <span class="show-on-open">open</span>
    <span class="show-on-close">close</span>
    </span>

    <button ng-click="isOpen = !isOpen">Toggle</button>
  </div>
</div>

NOTE: variable in JavaScript can't contains the dash character so rename your $scope.sign-open to $scope.signOpen

you don't even have to define a function in the controller, you can do it like this:

<span ng-class="{'open':!signOpen, '':signOpen}"></span>

on click handler <button ng-click="signOpen = !signOpen">toggle</button>

3 Comments

what about making it true and false on click
so please consider accepting the answer, if it answers your question
you are correct in your answer, but nikhil went for answer more fast, so upvote for you only is way for me to appreciate your good work.

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.