Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngAnimate on ngShow. Preventing animation when it starts first time

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?

like image 362
iLemming Avatar asked Apr 05 '13 17:04

iLemming


People also ask

Which of the following directives are associated with animate service?

AngularJS provides animation hooks for common directives such as ngRepeat, ngSwitch, and ngView, as well as custom directives via the $animate service.

Is angular animate part of AngularJS?

This package contains the legacy AngularJS (version 1. x). AngularJS support has officially ended as of January 2022.

What is angular animate JS?

The ngAnimate module does not animate your HTML elements, but when ngAnimate notice certain events, like hide or show of an HTML element, the element gets some pre-defined classes which can be used to make animations. The directives in AngularJS who add/remove classes are: ng-show. ng-hide.


2 Answers

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>

like image 74
TheLostBrain Avatar answered Dec 26 '22 20:12

TheLostBrain


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

like image 43
orjan Avatar answered Dec 26 '22 20:12

orjan