Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs toggle div visibility

I am trying to toggle a div text on click of a button. I tried taking a scope variable and toggeling classname based on the variable. Where am I making the mistake here

<button ng-click="toggle()">test</button>
<div ng-class="{{state}}">
  hello test
</div>
function ctrl($scope) {
  $scope.state = vis;
  $scope.toggle = function() {
    state = !state;
  };
}
.vis {
  display: none;
}
like image 500
Kurkula Avatar asked Sep 28 '15 19:09

Kurkula


People also ask

How to show and hide DIV on button click using AngularJS?

AngularJS is in control of showing/hiding that div. If you want to hide it by default, just set the value of scope. myvalue to false initially - which you're already doing. Lukas S.

How to show hide button in AngularJS?

$scope. ShowSave = true; If above is true it will display button and in case of false it hide your button.


1 Answers

You can simplify this a lot like so

<button ng-click="showDiv = !showDiv">test </button>
<div ng-show="showDiv" >
    hello test
</div>

Fiddle example

Unless you need the specific ng-class to toggle in which case you can do something like

<button ng-click="showDiv = !showDiv">test </button>
<div ng-class="{'vis' : showDiv }" >
    hello test
</div>

Fiddle example

(just make sure you're using a newer version of angular for this)

like image 94
ajmajmajma Avatar answered Oct 11 '22 23:10

ajmajmajma