Search code examples
angularjsangularjs-animation

How to prevent animation from running on element that is initially hidden?


My question is similar to this one and this one, but involves the new animations in AngularJS 1.2.0.

Basically, I have a bunch of elements on my signup page that are initially hidden and only displayed when a particular form element is invalid (they point to the invalid form element and display a message like "password needs to be at least 8 characters"). These messages fade in and out as they are shown/hidden.

But as soon as the signup page is displayed the elements are visible and fade out. They are briefly visible (they "blink" or "flash" on the screen, as is the case in situations where ng-cloak is necessary).

Here's a plunker to demonstrate the behaviour that I'm experiencing. In this plunker I have set up a basic route (the "login" page) that contains a box and a button that toggles the box's visibility. Notice how the box fades out when you click run? It should just be hidden. (I've deliberately set the animation to be slow so you can see the animation occurring).

How can I stop the animation from occurring initially?

What I've tried:

  • Set ng-cloak on the animating elements.
  • Use ng-cloak with the display: none !important rule added. (See this)
  • Setting display: none on the element, as if it were an "initial setting" for the element. (See this)

Interestingly, this plunker behaves properly and the animation doesn't occur initially. This plunker doesn't use routing and the controller is set explicitly on the body tag. I want to use routing though.


Solution

  • There's a problem with the version of angular you are using. Angular animate changed a lot I think in 1.2 so try this:

    <script src="http://code.angularjs.org/1.2.14/angular.min.js"></script>
    <script src="http://code.angularjs.org/1.2.14/angular-animate.min.js"></script>
    <script src="http://code.angularjs.org/1.2.14/angular-route.min.js"></script>
    

    Check it out, I've forked your plunker here

    http://plnkr.co/edit/Mchjx51GREGU2Gj0NBXX?p=preview