ngAnimate on ngShow. Preventing animation when it starts first time
Asked Answered
A

4

14

I was playing around with recently added angular.js animations feature, and this doesn't work as desired

<style>
    .myDiv{
        width:400px;
        height:200px;
        background-color:red;
    }
    .fadeIn-setup,.fadeOut-setup {
      -webkit-transition: 1s linear opacity;
      -moz-transition: 1s linear opacity;
      -o-transition: 1s linear opacity;
      transition: 1s linear opacity;
    }
    .fadeIn-setup{
      opacity:0;
    }
    .fadeOut-setup{
      opacity:1;
    }
    .fadeIn-setup.fadeIn-start {
      opacity: 1;
    }
    .fadeOut-setup.fadeOut-start{
        opacity:0;
    }
</style>

<div ng-app>
    <div ng-controller='ctrl'>
       <input type='button' value='click' ng-click='clicked()' />  
       <div ng-show="foo == true"  class='myDiv' ng-animate="{show: 'fadeIn', hide:'fadeOut'}">
       </div>
     </div>
</div>

function ctrl($scope){
    $scope.foo = false;
    $scope.clicked = function(){
       $scope.foo = !($scope.foo);
    }
}

http://jsfiddle.net/Kx4TS/1

it spoils away myDiv on the dom.ready and starts it fading out. Whereas it initially should be hidden. How to fix that?

Armin answered 5/4, 2013 at 17:1 Comment(2)
I figured: for my case it works if you make style of myDiv opacity:0. But it only works if I directly set the style, not through the class.Armin
I'm thinking this sort of thing should be done in a directive where you can hide the element initially and fade it in when necessary inside of a $watch callback.Shool
U
-1

This has been fixed now. Upgrade to 1.1.5.

Used answered 27/5, 2013 at 2:29 Comment(3)
This still persists in 1.1.5Harriot
I think they fixed it related with page load, see e.g. yearofmoo.com/2013/04/animation-in-angularjs.html: "Version 1.1.5 of AngularJS now properly avoids animating elements that are instantiated before bootstrap. So basically the first animation is skipped thus preventing any flickr or premature hiding." -- but it seems to still happen when loading fragments. The solution with style=display:none works for me in this case.Spiderwort
@ac360 you can add a ng-hide class on the element manually to hide it on initDeficiency
D
40

This issue still persists even now with 1.2.22. However, one of these solutions solves it very easily.

After trying all the solutions mentioned here I wanted to specifically highlight cocoggu's suggestion to ac360 as it is by far the most concise and it "just works".

As he suggests, simply adding the ng-hide class to the offending element resolves the problem immediately. - It prevents the initial animation glitch and allows all subsequent animations to behave as expected.

Working Solution thanks to cocoggu

<div id="offending-element" class="ng-hide"></div>

Disrobe answered 29/8, 2014 at 6:0 Comment(1)
Works well with angular 1.3.10 :)Aculeus
A
10

I have found 2 different solutions for your problem

The easiest solution: Add a inline style to the div style="display:none"

The other solution is to add an initial class to the div with ng-class="state" and reset the class when the button is clicked

function ctrl($scope){
    $scope.state = 'hide';
    $scope.foo = false;
    $scope.clicked = function() {
       $scope.state = undefined;  
       $scope.foo = !($scope.foo);
    }
}

together with the following css:

.hide {
    display: none;
} 

I think I would use the first and most simple solution

Anaconda answered 7/4, 2013 at 19:0 Comment(1)
style="display:none" worked for me (whereas class="ng-hide" did not). Running v1.5.6Uroscopy
W
0

this should do the trick:

<div ng-class="ng-hide" ng-show="foo == true"  class='myDiv'">

this is the important part here: ng-class="ng-hide"

EDIT: As pointed out in the comments, the above would not work. It should look like this:

<div class="ng-hide" ng-show="foo == true"  class='myDiv'">
Woodchuck answered 30/3, 2015 at 12:31 Comment(2)
@codeepic: what exactly does not work? could you please clarify? thxWoodchuck
Adding ng-class='ng-hide'. You want the ng-hide class to be applied before the Angular application starts, to prevent the flicker. You are better off setting class="ng-hide" on the element you want to be initially hidden. CSS is parsed before the JS, so in this case ng-hide class is applied before Angular app is initialized and the element is initially hidden without the annoying flicker.Recount
U
-1

This has been fixed now. Upgrade to 1.1.5.

Used answered 27/5, 2013 at 2:29 Comment(3)
This still persists in 1.1.5Harriot
I think they fixed it related with page load, see e.g. yearofmoo.com/2013/04/animation-in-angularjs.html: "Version 1.1.5 of AngularJS now properly avoids animating elements that are instantiated before bootstrap. So basically the first animation is skipped thus preventing any flickr or premature hiding." -- but it seems to still happen when loading fragments. The solution with style=display:none works for me in this case.Spiderwort
@ac360 you can add a ng-hide class on the element manually to hide it on initDeficiency

© 2022 - 2024 — McMap. All rights reserved.