@@ -425,6 +425,16 @@ angular.module('ngAnimate', ['ng'])
425425 element . data ( NG_ANIMATE_STATE , data ) ;
426426 }
427427
428+ function runAnimationPostDigest ( fn ) {
429+ var cancelFn ;
430+ $rootScope . $$postDigest ( function ( ) {
431+ cancelFn = fn ( ) ;
432+ } ) ;
433+ return function ( ) {
434+ cancelFn && cancelFn ( ) ;
435+ } ;
436+ }
437+
428438 function lookup ( name ) {
429439 if ( name ) {
430440 var matches = [ ] ,
@@ -648,6 +658,7 @@ angular.module('ngAnimate', ['ng'])
648658 * @param {DOMElement } parentElement the parent element of the element that will be the focus of the enter animation
649659 * @param {DOMElement } afterElement the sibling element (which is the previous element) of the element that will be the focus of the enter animation
650660 * @param {function()= } doneCallback the callback function that will be called once the animation is complete
661+ * @return {function } the animation cancellation function
651662 */
652663 enter : function ( element , parentElement , afterElement , doneCallback ) {
653664 element = angular . element ( element ) ;
@@ -656,9 +667,8 @@ angular.module('ngAnimate', ['ng'])
656667
657668 blockElementAnimations ( element ) ;
658669 $delegate . enter ( element , parentElement , afterElement ) ;
659- $rootScope . $$postDigest ( function ( ) {
660- element = stripCommentsFromElement ( element ) ;
661- performAnimation ( 'enter' , 'ng-enter' , element , parentElement , afterElement , noop , doneCallback ) ;
670+ return runAnimationPostDigest ( function ( ) {
671+ return performAnimation ( 'enter' , 'ng-enter' , stripCommentsFromElement ( element ) , parentElement , afterElement , noop , doneCallback ) ;
662672 } ) ;
663673 } ,
664674
@@ -691,13 +701,16 @@ angular.module('ngAnimate', ['ng'])
691701 *
692702 * @param {DOMElement } element the element that will be the focus of the leave animation
693703 * @param {function()= } doneCallback the callback function that will be called once the animation is complete
704+ * @return {function } the animation cancellation function
694705 */
695706 leave : function ( element , doneCallback ) {
696707 element = angular . element ( element ) ;
708+
697709 cancelChildAnimations ( element ) ;
698710 blockElementAnimations ( element ) ;
699- $rootScope . $$postDigest ( function ( ) {
700- performAnimation ( 'leave' , 'ng-leave' , stripCommentsFromElement ( element ) , null , null , function ( ) {
711+ this . enabled ( false , element ) ;
712+ return runAnimationPostDigest ( function ( ) {
713+ return performAnimation ( 'leave' , 'ng-leave' , stripCommentsFromElement ( element ) , null , null , function ( ) {
701714 $delegate . leave ( element ) ;
702715 } , doneCallback ) ;
703716 } ) ;
@@ -735,6 +748,7 @@ angular.module('ngAnimate', ['ng'])
735748 * @param {DOMElement } parentElement the parentElement element of the element that will be the focus of the move animation
736749 * @param {DOMElement } afterElement the sibling element (which is the previous element) of the element that will be the focus of the move animation
737750 * @param {function()= } doneCallback the callback function that will be called once the animation is complete
751+ * @return {function } the animation cancellation function
738752 */
739753 move : function ( element , parentElement , afterElement , doneCallback ) {
740754 element = angular . element ( element ) ;
@@ -744,9 +758,8 @@ angular.module('ngAnimate', ['ng'])
744758 cancelChildAnimations ( element ) ;
745759 blockElementAnimations ( element ) ;
746760 $delegate . move ( element , parentElement , afterElement ) ;
747- $rootScope . $$postDigest ( function ( ) {
748- element = stripCommentsFromElement ( element ) ;
749- performAnimation ( 'move' , 'ng-move' , element , parentElement , afterElement , noop , doneCallback ) ;
761+ return runAnimationPostDigest ( function ( ) {
762+ return performAnimation ( 'move' , 'ng-move' , stripCommentsFromElement ( element ) , parentElement , afterElement , noop , doneCallback ) ;
750763 } ) ;
751764 } ,
752765
@@ -778,11 +791,12 @@ angular.module('ngAnimate', ['ng'])
778791 * @param {DOMElement } element the element that will be animated
779792 * @param {string } className the CSS class that will be added to the element and then animated
780793 * @param {function()= } doneCallback the callback function that will be called once the animation is complete
794+ * @return {function } the animation cancellation function
781795 */
782796 addClass : function ( element , className , doneCallback ) {
783797 element = angular . element ( element ) ;
784798 element = stripCommentsFromElement ( element ) ;
785- performAnimation ( 'addClass' , className , element , null , null , function ( ) {
799+ return performAnimation ( 'addClass' , className , element , null , null , function ( ) {
786800 $delegate . addClass ( element , className ) ;
787801 } , doneCallback ) ;
788802 } ,
@@ -815,11 +829,12 @@ angular.module('ngAnimate', ['ng'])
815829 * @param {DOMElement } element the element that will be animated
816830 * @param {string } className the CSS class that will be animated and then removed from the element
817831 * @param {function()= } doneCallback the callback function that will be called once the animation is complete
832+ * @return {function } the animation cancellation function
818833 */
819834 removeClass : function ( element , className , doneCallback ) {
820835 element = angular . element ( element ) ;
821836 element = stripCommentsFromElement ( element ) ;
822- performAnimation ( 'removeClass' , className , element , null , null , function ( ) {
837+ return performAnimation ( 'removeClass' , className , element , null , null , function ( ) {
823838 $delegate . removeClass ( element , className ) ;
824839 } , doneCallback ) ;
825840 } ,
@@ -848,13 +863,14 @@ angular.module('ngAnimate', ['ng'])
848863 * removed from it
849864 * @param {string } add the CSS classes which will be added to the element
850865 * @param {string } remove the CSS class which will be removed from the element
851- * @param {Function = } done the callback function (if provided) that will be fired after the
866+ * @param {function = } done the callback function (if provided) that will be fired after the
852867 * CSS classes have been set on the element
868+ * @return {function } the animation cancellation function
853869 */
854870 setClass : function ( element , add , remove , doneCallback ) {
855871 element = angular . element ( element ) ;
856872 element = stripCommentsFromElement ( element ) ;
857- performAnimation ( 'setClass' , [ add , remove ] , element , null , null , function ( ) {
873+ return performAnimation ( 'setClass' , [ add , remove ] , element , null , null , function ( ) {
858874 $delegate . setClass ( element , add , remove ) ;
859875 } , doneCallback ) ;
860876 } ,
@@ -905,13 +921,14 @@ angular.module('ngAnimate', ['ng'])
905921 */
906922 function performAnimation ( animationEvent , className , element , parentElement , afterElement , domOperation , doneCallback ) {
907923
924+ var noopCancel = noop ;
908925 var runner = animationRunner ( element , animationEvent , className ) ;
909926 if ( ! runner ) {
910927 fireDOMOperation ( ) ;
911928 fireBeforeCallbackAsync ( ) ;
912929 fireAfterCallbackAsync ( ) ;
913930 closeAnimation ( ) ;
914- return ;
931+ return noopCancel ;
915932 }
916933
917934 className = runner . className ;
@@ -945,7 +962,7 @@ angular.module('ngAnimate', ['ng'])
945962 fireBeforeCallbackAsync ( ) ;
946963 fireAfterCallbackAsync ( ) ;
947964 closeAnimation ( ) ;
948- return ;
965+ return noopCancel ;
949966 }
950967
951968 var skipAnimation = false ;
@@ -993,7 +1010,7 @@ angular.module('ngAnimate', ['ng'])
9931010 fireBeforeCallbackAsync ( ) ;
9941011 fireAfterCallbackAsync ( ) ;
9951012 fireDoneCallbackAsync ( ) ;
996- return ;
1013+ return noopCancel ;
9971014 }
9981015
9991016 if ( animationEvent == 'leave' ) {
@@ -1046,6 +1063,8 @@ angular.module('ngAnimate', ['ng'])
10461063 }
10471064 } ) ;
10481065
1066+ return runner . cancel ;
1067+
10491068 function fireDOMCallback ( animationPhase ) {
10501069 var eventName = '$animate:' + animationPhase ;
10511070 if ( elementEvents && elementEvents [ eventName ] && elementEvents [ eventName ] . length > 0 ) {
0 commit comments