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 $watch
ing 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?
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.
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.
Animations within custom directives can also be established by injecting $animate directly into your directive and making calls to it when needed.
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.
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With