showdiv

Show Hide Div Element Using AngularJS

Spread the love

We can animate a div to show/hide it using angularjs. What we have done here is decorated the body with ng-app=”ngAnimate”, binded the checkbox with ng-model=”myCheck”and decorated the div with ng-hide=”myCheck”. Apart from this we have created two style classes, one if for how the div should look named “div” class and other is how the div will hide named ng-hide. And that’s it. The complete code is shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html>
<style>
div {
  transition: all linear 0.5s;
  background-color: #EEEEEE;
  height: 200px;
  width: 100%;
  position: relative;
  top: 0;
  left: 0;
}
 
.ng-hide {
  height: 0;
  width: 0;
  background-color: transparent;
  top:-200px;
  left: 200px;
}
 
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-animate.js"></script>
 
<body ng-app="ngAnimate">
 
<h1>Hide the DIV: <input type="checkbox" ng-model="myCheck"></h1>
 
<div ng-hide="myCheck"></div>
 
</body>
</html>

The Show/Hide effects on the div are shown in the below screenshots:

showdiv

hidediv


Leave a Reply

Your email address will not be published. Required fields are marked *