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?
@michal-charemza solution works great, but the directive creates an isolated scope, so in some cases it cannot be a direct replacement for the default ng-show directive.
I have modified it a bit, so that it does not create any new scopes and can be used interchangeably with the ng-show directive.
Usage:
Plunker
@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:
Plunker: Code Example
As @zeroflagL has suggested, a custom directive to replace
ngShow
is probably the way to go. You can use&
to pass callbacks into the directive, which can be called after the animations have finished. For consistency, the animations are done by adding and removing theng-hide
class, which is the same method used by the usual ngShow directive:Example use of this listening to a scope variable
show
would be:Because this is adding/removing the ng-hide class, the points about animating from the docs about ngShow are still valid, and you need to add
display: block !important
to the CSS.You can see an example of this in action at this Plunker.