0

I am new to angularjs and am trying to learn how the framework works. Here is my problem. I wrote a script to have a button showing and hiding the message. At the same time I tried to act animation when the message was being displayed. Unfortunately, I can either do one of them. As long as I combine the both in the script, I don't know why I have to remove the "hide" button to get the animation work.

Here's my script.


test.html:

<!-- test.html -->
<!DOCTYPE html>
<html ng-app='test'>

<head>
  <title>test angular animation</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-animate.js"></script>
  <script src="test.js"></script>
  <style>
    div.ng-enter {
      transition: 2s linear all;
      opacity: 0;
    }

    div.ng-enter.ng-enter-active {
      opacity: 1;
    }
  </style>
</head>

<body ng-controller="testController as doc">
  <!-- TO MAKE THE ANIMATION WORK, THE FOLLOWING HIDE BUTTON HAVE TO BE COMMENTED, PLEASE HELP -->
  <button ng-if="doc.div_show" ng-click="doc.div_show=false">hide</button>
  <button ng-if="!doc.div_show" ng-click="doc.div_show=true">show</button>
  <div ng-if="doc.div_show">Hello World</div>
</body>

</html>

test.js:

/* test.js */
angular.module('test', ['ngAnimate'])
  .controller('testController', function() {
    var doc = this;
    doc.div_show = false;
  });

1 Answer 1

1

Change your style just as shown in angular docs, like this:

<style>
/* The starting CSS styles for the enter animation */
.fade.ng-enter {
    transition:0.5s linear all;
    opacity:0;
}

/* The finishing CSS styles for the enter animation */
.fade.ng-enter.ng-enter-active {
    opacity:1;
}
</style>

Add a fade class to your div:

<div ng-if="doc.div_show" class="fade">Hello World</div>
Sign up to request clarification or add additional context in comments.

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.