NgIf with ngAnimate - duplicated HTML content during animation

I am trying to make a simple angular animation. But I have a problem, when I quickly click the buttons (until the animation finishes), the content containers are created so many times when I clicked ...

How to prevent this situation? I want to stop the animation after a click, and then run the animation on the same object, and not on the new one?

JSFIDDLE

HTML

 <div ng-app="test">
    <div class="container" ng-controller="TestController as tCtrl">
        <div class="row">
            <div class="col-xs-12">                
                <a class="btn btn-default" ng-click="tCtrl.tt = 1"> 1 </a>
                <a class="btn btn-default" ng-click="tCtrl.tt = 2"> 2 </a>
            </div>
        </div>       
        <div class="row">
            <div class="col-xs-12 animate-if" ng-if="tCtrl.tt === 1">Chosen = 1</div>
            <div class="col-xs-12 animate-if" ng-if="tCtrl.tt === 2">Chosen = 2</div>
        </div>
    </div>
</div>

Js

var app = angular.module('test', ['ngAnimate']);


app.controller('TestController', function ($scope) {
    this.tt = 1;
});

CSS

 .animate-if.ng-enter, .animate-if.ng-leave {    
     -webkit-transition: opacity 5.2s ease-in-out;
     -moz-transition: opacity 5.2s ease-in-out;
     -ms-transition: opacity 5.2s ease-in-out;
     transition: opacity 5.2s ease-in-out;  
 }
 .animate-if.ng-enter,
 .animate-if.ng-leave.ng-leave-active {
     z-index: 0;    
     opacity: 0;
 }

 .animate-if.ng-leave,
 .animate-if.ng-enter.ng-enter-active {
     z-index: 1;
     opacity: 1;
 }

EDIT:

I used the code to use ngRoutesand ngViewinstead ngIf, but the behavior is the ngAnimatesame. I think this may be an error in angular because in the console I have an error undefined in not a functionin angular -animate.js on line 1348, which contains operation.cancel()and is part of:

  forEach(animationsToCancel, function(operation) {
       operation.cancel();
  });

, " " "", .

2:

, :

 this.fn_change_view = function (str_view, $event) {
        if (!this.contentStopAnim) {
            this.contentStopAnim = true;
            this.active_view = str_view;
            $timeout(function () {
                template.contentStopAnim = false;
            }, 360);
        } else {
            $event.preventDefault();
        }
    };

href , , ...

+4
1

ng-show . plunker.

css.

.ng-hide-add {
  animation:0.1s keyFrameLeave ease; 
}

.ng-hide-remove { 
  animation:0.5s keyFrameEnter ease; 
}

@keyframes keyFrameEnter {
  0% {
    transform: translate(0,100px);
    opacity: 0;
  }
  100% {
    transform: translate(0,0);
    opacity: 1;
  }
}

@keyframes keyFrameLeave {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

html :

<button ng-click="front = !front">click to animate</button>
<div ng-show="front">Displayed</div>

js

$scope.front = false;
+2

Source: https://habr.com/ru/post/1569820/


All Articles