Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running code after an AngularJS animation has completed

Tags:

I have an element whose visibility is toggled by ng-show. I'm also using CSS animations - the automatic ones from ng-animate - on this element to animate its entry.

The element will either contain an image or a video.

In the case that the element contains a video, I want to play it, but I don't want to play the video until it's finished animating in.

As such, I was wondering if there's an easy way to bind a callback to the end of a CSS animation in AngularJS?

The docs reference a doneCallback, but I can't see a way to specify it...

One workaround(?) I have thought of is $watching element.hasClass("ng-hide-add-active") and waiting for it to fire with (true, false), implying it's just been removed..

Is there a nicer way?

like image 743
Ed_ Avatar asked Jan 04 '14 13:01

Ed_


People also ask

How to use animate in AngularJS?

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.

Which library do we include to implement animations in AngularJS?

To use animations, we need to include the angular-animate. js library and add the module ngAnimate as a dependency in the module, as shown below.

Can animations be achieved with custom directive?

Animations within custom directives can also be established by injecting $animate directly into your directive and making calls to it when needed.

What is ngAnimate?

The ngAnimate module provides support for CSS-based animations (keyframes and transitions) as well as JavaScript-based animations via callback hooks. Animations are not enabled by default, however, by including ngAnimate the animation hooks are enabled for an AngularJS app.


1 Answers

@michael-charemza answer worked great for me. If you are using Angular 1.3 they changed the promise a little. I got stuck on this for a little bit but here is the change that got it to work:

if (show) {   $animate.removeClass(element, 'ng-hide').then(scope.afterShow); } if (!show) {   $animate.addClass(element, 'ng-hide').then(scope.afterHide); } 

Plunker: Code Example

like image 74
Lereveme Avatar answered Oct 13 '22 00:10

Lereveme