Improve this Doc View Source $animate
- $animateProvider
 - service in module ngAnimate
 
The $animate service provides animation detection support while performing DOM operations (enter, leave and move) as well as during addClass and removeClass operations. When any of these operations are run, the $animate service will examine any JavaScript-defined animations (which are defined by using the $animateProvider provider object) as well as any CSS-defined animations against the CSS classes present on the element once the DOM operation is run.
The $animate service is used behind the scenes with pre-existing directives and animation with these directives will work out of the box without any extra configuration.
Requires the ngAnimate module to be installed.
Please visit the ngAnimate module overview page learn more about how to use animations in your application.
Callback Promises
With AngularJS 1.3, each of the animation methods, on the $animate service, return a promise when called. The promise itself is then resolved once the animation has completed itself, has been cancelled or has been skipped due to animations being disabled. (Note that even if the animation is cancelled it will still call the resolve function of the animation.)
$animate.enter(element, container).then(function() {
  //...this is called once the animation is complete...
});
 Also note that, due to the nature of the callback promise, if any Angular-specific code (like changing the scope, location of the page, etc...) is executed within the callback promise then be sure to wrap the code using $scope.$apply(...);
$animate.leave(element).then(function() {
  $scope.$apply(function() {
    $location.path('/new-page');
  });
});
 An animation can also be cancelled by calling the $animate.cancel(promise) method with the provided promise that was returned when the animation was started.
var promise = $animate.addClass(element, 'super-long-animation');
promise.then(function() {
  //this will still be called even if cancelled
});
element.on('click', function() {
  //tooo lazy to wait for the animation to end
  $animate.cancel(promise);
});
 (Keep in mind that the promise cancellation is unique to $animate since promises in general cannot be cancelled.)
Methods
-  
animate(element, from, to, [className], [options]);
Performs an inline animation on the element which applies the provided
toandfromCSS styles to the element. If any detected CSS transition, keyframe or JavaScript matches the providedclassNamevalue then the animation will take on the provided styles. For example, if a transition animation is set for the given className then the providedfromandtostyles will be applied alongside the given transition. If a JavaScript animation is detected then the provided styles will be given in as function paramters.ngModule.animation('.my-inline-animation', function() { return { animate : function(element, className, from, to, done) { //styles } } });Below is a breakdown of each step that occurs during the
animateanimation:Animation Step What the element class attribute looks like 1. $animate.animate(...)is calledclass="my-animation"2. $animatewaits for the next digest to start the animationclass="my-animation ng-animate"3. $animateruns the JavaScript-defined animations detected on the elementclass="my-animation ng-animate"4. the classNameclass value is added to the elementclass="my-animation ng-animate className"5. $animatescans the element styles to get the CSS transition/animation duration and delayclass="my-animation ng-animate className"6. $animateblocks all CSS transitions on the element to ensure the.classNameclass styling is applied right awayclass="my-animation ng-animate className"7. $animateapplies the provided collection offromCSS styles to the elementclass="my-animation ng-animate className"8. $animatewaits for a single animation frame (this performs a reflow)class="my-animation ng-animate className"9. $animateremoves the CSS transition block placed on the elementclass="my-animation ng-animate className"10. the className-activeclass is added (this triggers the CSS transition/animation)class="my-animation ng-animate className className-active"11. $animateapplies the collection oftoCSS styles to the element which are then handled by the transitionclass="my-animation ng-animate className className-active"12. $animatewaits for the animation to complete (via events and timeout)class="my-animation ng-animate className className-active"13. The animation ends and all generated CSS classes are removed from the element class="my-animation"14. The returned promise is resolved. class="my-animation"Parameters
Param Type Details element DOMElementthe element that will be the focus of the enter animation
from objecta collection of CSS styles that will be applied to the element at the start of the animation
to objecta collection of CSS styles that the element will animate towards
className (optional)stringan optional CSS class that will be added to the element for the duration of the animation (the default class is
ng-inline-animate)options (optional)objectan optional collection of options that will be picked up by the CSS transition/animation
Returns
Promisethe animation callback promise
 -  
enter(element, parentElement, afterElement, [options]);
Appends the element to the parentElement element that resides in the document and then runs the enter animation. Once the animation is started, the following CSS classes will be present on the element for the duration of the animation:
Below is a breakdown of each step that occurs during enter animation:
Animation Step What the element class attribute looks like 1. $animate.enter(...)is calledclass="my-animation"2. element is inserted into the parentElementelement or beside theafterElementelementclass="my-animation"3. $animatewaits for the next digest to start the animationclass="my-animation ng-animate"4. $animateruns the JavaScript-defined animations detected on the elementclass="my-animation ng-animate"5. the .ng-enterclass is added to the elementclass="my-animation ng-animate ng-enter"6. $animatescans the element styles to get the CSS transition/animation duration and delayclass="my-animation ng-animate ng-enter"7. $animateblocks all CSS transitions on the element to ensure the.ng-enterclass styling is applied right awayclass="my-animation ng-animate ng-enter"8. $animatewaits for a single animation frame (this performs a reflow)class="my-animation ng-animate ng-enter"9. $animateremoves the CSS transition block placed on the elementclass="my-animation ng-animate ng-enter"10. the .ng-enter-activeclass is added (this triggers the CSS transition/animation)class="my-animation ng-animate ng-enter ng-enter-active"11. $animatewaits for the animation to complete (via events and timeout)class="my-animation ng-animate ng-enter ng-enter-active"12. The animation ends and all generated CSS classes are removed from the element class="my-animation"13. The returned promise is resolved. class="my-animation"Parameters
Param Type Details element DOMElementthe element that will be the focus of the enter animation
parentElement DOMElementthe parent element of the element that will be the focus of the enter animation
afterElement DOMElementthe sibling element (which is the previous element) of the element that will be the focus of the enter animation
options (optional)objectan optional collection of options that will be picked up by the CSS transition/animation
Returns
Promisethe animation callback promise
 -  
leave(element, [options]);
Runs the leave animation operation and, upon completion, removes the element from the DOM. Once the animation is started, the following CSS classes will be added for the duration of the animation:
Below is a breakdown of each step that occurs during leave animation:
Animation Step What the element class attribute looks like 1. $animate.leave(...)is calledclass="my-animation"2. $animateruns the JavaScript-defined animations detected on the elementclass="my-animation ng-animate"3. $animatewaits for the next digest to start the animationclass="my-animation ng-animate"4. the .ng-leaveclass is added to the elementclass="my-animation ng-animate ng-leave"5. $animatescans the element styles to get the CSS transition/animation duration and delayclass="my-animation ng-animate ng-leave"6. $animateblocks all CSS transitions on the element to ensure the.ng-leaveclass styling is applied right awayclass="my-animation ng-animate ng-leave"7. $animatewaits for a single animation frame (this performs a reflow)class="my-animation ng-animate ng-leave"8. $animateremoves the CSS transition block placed on the elementclass="my-animation ng-animate ng-leave"9. the .ng-leave-activeclass is added (this triggers the CSS transition/animation)class="my-animation ng-animate ng-leave ng-leave-active"10. $animatewaits for the animation to complete (via events and timeout)class="my-animation ng-animate ng-leave ng-leave-active"11. The animation ends and all generated CSS classes are removed from the element class="my-animation"12. The element is removed from the DOM ... 13. The returned promise is resolved. ... Parameters
Param Type Details element DOMElementthe element that will be the focus of the leave animation
options (optional)objectan optional collection of styles that will be picked up by the CSS transition/animation
Returns
Promisethe animation callback promise
 -  
move(element, parentElement, afterElement, [options]);
Fires the move DOM operation. Just before the animation starts, the animate service will either append it into the parentElement container or add the element directly after the afterElement element if present. Then the move animation will be run. Once the animation is started, the following CSS classes will be added for the duration of the animation:
Below is a breakdown of each step that occurs during move animation:
Animation Step What the element class attribute looks like 1. $animate.move(...)is calledclass="my-animation"2. element is moved into the parentElement element or beside the afterElement element class="my-animation"3. $animatewaits for the next digest to start the animationclass="my-animation ng-animate"4. $animateruns the JavaScript-defined animations detected on the elementclass="my-animation ng-animate"5. the .ng-moveclass is added to the elementclass="my-animation ng-animate ng-move"6. $animatescans the element styles to get the CSS transition/animation duration and delayclass="my-animation ng-animate ng-move"7. $animateblocks all CSS transitions on the element to ensure the.ng-moveclass styling is applied right awayclass="my-animation ng-animate ng-move"8. $animatewaits for a single animation frame (this performs a reflow)class="my-animation ng-animate ng-move"9. $animateremoves the CSS transition block placed on the elementclass="my-animation ng-animate ng-move"10. the .ng-move-activeclass is added (this triggers the CSS transition/animation)class="my-animation ng-animate ng-move ng-move-active"11. $animatewaits for the animation to complete (via events and timeout)class="my-animation ng-animate ng-move ng-move-active"12. The animation ends and all generated CSS classes are removed from the element class="my-animation"13. The returned promise is resolved. class="my-animation"Parameters
Param Type Details element DOMElementthe element that will be the focus of the move animation
parentElement DOMElementthe parentElement element of the element that will be the focus of the move animation
afterElement DOMElementthe sibling element (which is the previous element) of the element that will be the focus of the move animation
options (optional)objectan optional collection of styles that will be picked up by the CSS transition/animation
Returns
Promisethe animation callback promise
 -  
addClass(element, className, [options]);
Triggers a custom animation event based off the className variable and then attaches the className value to the element as a CSS class. Unlike the other animation methods, the animate service will suffix the className value with
-addin order to provide the animate service the setup and active CSS classes in order to trigger the animation (this will be skipped if no CSS transitions or keyframes are defined on the -add-active or base CSS class).Below is a breakdown of each step that occurs during addClass animation:
Animation Step What the element class attribute looks like 1. $animate.addClass(element, 'super')is calledclass="my-animation"2. $animateruns the JavaScript-defined animations detected on the elementclass="my-animation ng-animate"3. the .super-addclass is added to the elementclass="my-animation ng-animate super-add"4. $animatewaits for a single animation frame (this performs a reflow)class="my-animation ng-animate super-add"5. the .superand.super-add-activeclasses are added (this triggers the CSS transition/animation)class="my-animation ng-animate super super-add super-add-active"6. $animatescans the element styles to get the CSS transition/animation duration and delayclass="my-animation ng-animate super super-add super-add-active"7. $animatewaits for the animation to complete (via events and timeout)class="my-animation ng-animate super super-add super-add-active"8. The animation ends and all generated CSS classes are removed from the element class="my-animation super"9. The super class is kept on the element class="my-animation super"10. The returned promise is resolved. class="my-animation super"Parameters
Param Type Details element DOMElementthe element that will be animated
className stringthe CSS class that will be added to the element and then animated
options (optional)objectan optional collection of styles that will be picked up by the CSS transition/animation
Returns
Promisethe animation callback promise
 -  
removeClass(element, className, [options]);
Triggers a custom animation event based off the className variable and then removes the CSS class provided by the className value from the element. Unlike the other animation methods, the animate service will suffix the className value with
-removein order to provide the animate service the setup and active CSS classes in order to trigger the animation (this will be skipped if no CSS transitions or keyframes are defined on the -remove or base CSS classes).Below is a breakdown of each step that occurs during removeClass animation:
Animation Step What the element class attribute looks like 1. $animate.removeClass(element, 'super')is calledclass="my-animation super"2. $animateruns the JavaScript-defined animations detected on the elementclass="my-animation super ng-animate"3. the .super-removeclass is added to the elementclass="my-animation super ng-animate super-remove"4. $animatewaits for a single animation frame (this performs a reflow)class="my-animation super ng-animate super-remove"5. the .super-remove-activeclasses are added and.superis removed (this triggers the CSS transition/animation)class="my-animation ng-animate super-remove super-remove-active"6. $animatescans the element styles to get the CSS transition/animation duration and delayclass="my-animation ng-animate super-remove super-remove-active"7. $animatewaits for the animation to complete (via events and timeout)class="my-animation ng-animate super-remove super-remove-active"8. The animation ends and all generated CSS classes are removed from the element class="my-animation"9. The returned promise is resolved. class="my-animation"Parameters
Param Type Details element DOMElementthe element that will be animated
className stringthe CSS class that will be animated and then removed from the element
options (optional)objectan optional collection of styles that will be picked up by the CSS transition/animation
Returns
Promisethe animation callback promise
 -  
setClass(element, add, remove, [options]);
Adds and/or removes the given CSS classes to and from the element. Once complete, the
done()callback will be fired (if provided).Animation Step What the element class attribute looks like 1. $animate.setClass(element, 'on', 'off')is calledclass="my-animation off"2. $animateruns the JavaScript-defined animations detected on the elementclass="my-animation ng-animate off"3. the .on-addand.off-removeclasses are added to the elementclass="my-animation ng-animate on-add off-remove off"4. $animatewaits for a single animation frame (this performs a reflow)class="my-animation ng-animate on-add off-remove off"5. the .on,.on-add-activeand.off-remove-activeclasses are added and.offis removed (this triggers the CSS transition/animation)class="my-animation ng-animate on on-add on-add-active off-remove off-remove-active"6. $animatescans the element styles to get the CSS transition/animation duration and delayclass="my-animation ng-animate on on-add on-add-active off-remove off-remove-active"7. $animatewaits for the animation to complete (via events and timeout)class="my-animation ng-animate on on-add on-add-active off-remove off-remove-active"8. The animation ends and all generated CSS classes are removed from the element class="my-animation on"9. The returned promise is resolved. class="my-animation on"Parameters
Param Type Details element DOMElementthe element which will have its CSS classes changed removed from it
add stringthe CSS classes which will be added to the element
remove stringthe CSS class which will be removed from the element CSS classes have been set on the element
options (optional)objectan optional collection of styles that will be picked up by the CSS transition/animation
Returns
Promisethe animation callback promise
 -  
cancel(animationPromise);
Cancels the provided animation.
Parameters
Param Type Details animationPromise PromiseThe animation promise that is returned when an animation is started.
 -  
enabled([value], [element]);
Globally enables/disables animations.
Parameters
Param Type Details value (optional)booleanIf provided then set the animation on or off.
element (optional)DOMElementIf provided then the element will be used to represent the enable/disable operation
Returns
booleanCurrent animation state.
 
    © 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
    https://code.angularjs.org/1.3.20/docs/api/ngAnimate/service/$animate