AngularJS Show/Hide Animation using ngAnimate

Step 1 : Add Dependency

In the header portion of your HTML, add the following:
  1. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  2. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  3. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-animate.min.js"></script>
Take note that your angular-animate version should be the same as your angular version.

Step 2 : Add Style

Include the following style.
  1. .sample-animation {
  2. transition: all linear 0.5s;
  3. }
  4. .sample-animation.ng-hide {
  5. opacity: 0;
  6. }
  7. #alertDiv{
  8. margin-top: 10px;
  9. }

Step 3 : Creating the Body

This contains our sample div to hide/show to show our animation.
  1. <div class="container">
  2. <h1 class="page-header text-center">AngularJS Show/Hide Animation using ngAnimate</h1>
  3. <div class="text-center">
  4. <button type="button" class="btn btn-primary btn-lg" ng-click="show()">{{ action }}</button>
  5. </div>
  6. <div class="row" id="alertDiv">
  7. <div class="col-sm-6 col-sm-offset-3">
  8. <div class="alert alert-success text-center sample-animation" ng-show="myAlert">
  9. <button type="button" class="close" ng-click="hide()"><span aria-hidden="true">&times;</span></button>
  10. This is a sample alert
  11. </div>
  12. </div>
  13. </div>
  14. </div>

Step 4 : Add the Angular JS script

Lastly, this is our angular js script.
  1. var app = angular.module('app', ['ngAnimate']);
  2.  
  3. app.controller('myCtrl', function($scope){
  4. $scope.myAlert = false;
  5. $scope.action = 'Show';
  6.  
  7. $scope.show = function(){
  8. if($scope.myAlert == false){
  9. $scope.myAlert = true;
  10. $scope.action = 'Hide';
  11. }
  12. else{
  13. $scope.hide();
  14. }
  15.  
  16. }
  17.  
  18. $scope.hide = function(){
  19. $scope.myAlert = false;
  20. $scope.action = 'Show';
  21. }
  22. });
Don't forget to include the dependency of ngAnimate in your app.

Full HTML

  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3. <title>AngularJS Show/Hide Animation using ngAnimate</title>
  4. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  5. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-animate.min.js"></script>
  7. <style type="text/css">
  8. .sample-animation {
  9. transition: all linear 0.5s;
  10. }
  11. .sample-animation.ng-hide {
  12. opacity: 0;
  13. }
  14. #alertDiv{
  15. margin-top: 10px;
  16. }
  17. </style>
  18. </head>
  19. <body ng-controller="myCtrl">
  20. <div class="container">
  21. <h1 class="page-header text-center">AngularJS Show/Hide Animation using ngAnimate</h1>
  22. <div class="text-center">
  23. <button type="button" class="btn btn-primary btn-lg" ng-click="show()">{{ action }}</button>
  24. </div>
  25. <div class="row" id="alertDiv">
  26. <div class="col-sm-6 col-sm-offset-3">
  27. <div class="alert alert-success text-center sample-animation" ng-show="myAlert">
  28. <button type="button" class="close" ng-click="hide()"><span aria-hidden="true">&times;</span></button>
  29. This is a sample alert
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. var app = angular.module('app', ['ngAnimate']);
  35.  
  36. app.controller('myCtrl', function($scope){
  37. $scope.myAlert = false;
  38. $scope.action = 'Show';
  39.  
  40. $scope.show = function(){
  41. if($scope.myAlert == false){
  42. $scope.myAlert = true;
  43. $scope.action = 'Hide';
  44. }
  45. else{
  46. $scope.hide();
  47. }
  48.  
  49. }
  50.  
  51. $scope.hide = function(){
  52. $scope.myAlert = false;
  53. $scope.action = 'Show';
  54. }
  55. });
  56. </body>
  57. </html>
That ends this tutorial. Happy Coding :)

Add new comment