Improve this Doc View Source

## Overview

The $animate service exposes a series of DOM utility methods that provide support for animation hooks. The default behavior is the application of DOM operations, however, when an animation is detected (and animations are enabled), $animate will do the heavy lifting to ensure that animation runs with the triggered DOM operation.

By default $animate doesn't trigger any animations. This is because the `ngAnimate` module isn't included and only when it is active then the animation hooks that `$animate` triggers will be functional. Once active then all structural `ng-` directives will trigger animations as they perform their DOM-related operations (enter, leave and move). Other directives such as `ngClass`, `ngShow`, `ngHide` and `ngMessages` also provide support for animations.

It is recommended that the`$animate` service is always used when executing DOM-related procedures within directives.

To learn more about enabling animation support, click here to visit the ngAnimate module page.

## Methods

- ###

  `on(event, container, callback);`

  Sets up an event listener to fire whenever the animation event (enter, leave, move, etc...) has fired on the given element or among any of its children. Once the listener is fired, the provided callback is fired with the following params:

      $animate.on('enter', container,
         function callback(element, phase) {
           // cool we detected an enter animation within the container
         }
      );

  Note: Generally, the events that are fired correspond 1:1 to `$animate` method names, e.g. addClass() will fire `addClass`, and `ngClass` will fire `addClass` if classes are added, and `removeClass` if classes are removed. However, there are two exceptions:
  - if both an addClass() and a removeClass() action are performed during the same animation, the event fired will be `setClass`. This is true even for `ngClass`.
  - an animate() call that adds and removes classes will fire the `setClass` event, but if it either removes or adds classes, it will fire `animate` instead.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  event | string | the animation event that will be captured (e.g. enter, leave, move, addClass, removeClass, etc...)
  container | DOMElement | the container element that will capture each of the animation events that are fired on itself as well as among its children
  callback | Function | the callback function that will be fired when the listener is triggered. The arguments present in the callback function are: `element` - The captured DOM element that the animation was fired on. `phase` - The phase of the animation. The two possible phases are start (when the animation starts) and close (when it ends). `data` - an object with these properties: addClass - `{string|null}` - space-separated CSS classes to add to the element removeClass - `{string|null}` - space-separated CSS classes to remove from the element from - `{Object|null}` - CSS properties & values at the beginning of the animation to - `{Object|null}` - CSS properties & values at the end of the animation Note that the callback does not trigger a scope digest. Wrap your call into a scope.$apply to propagate changes to the scope.

- ###

  `off(event, [container], [callback]);`

  Deregisters an event listener based on the event which has been associated with the provided element. This method can be used in three different ways depending on the arguments:

      // remove all the animation event listeners listening for `enter`
      $animate.off('enter');

      // remove listeners for all animation events from the container element
      $animate.off(container);

      // remove all the animation event listeners listening for `enter` on the given element and its children
      $animate.off('enter', container);

      // remove the event listener function provided by `callback` that is set
      // to listen for `enter` on the given `container` as well as its children
      $animate.off('enter', container, callback);

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  event | container | string DOMElement | the animation event (e.g. enter, leave, move, addClass, removeClass, etc...), or the container element. If it is the element, all other arguments are ignored.
  container (optional) | DOMElement | the container element the event listener was placed on
  callback (optional) | Function= | the callback function that was registered as the listener

- ###

  `pin(element, parentElement);`

  Associates the provided element with a host parent element to allow the element to be animated even if it exists outside of the DOM structure of the AngularJS application. By doing so, any animation triggered via `$animate` can be issued on the element despite being outside the realm of the application or within another application. Say for example if the application was bootstrapped on an element that is somewhere inside of the `<body>` tag, but we wanted to allow for an element to be situated as a direct child of `document.body`, then this can be achieved by pinning the element via `$animate.pin(element)`. Keep in mind that calling `$animate.pin(element, parentElement)` will not actually insert into the DOM anywhere; it will just create the association.

  Note that this feature is only active when the `ngAnimate` module is used.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  element | DOMElement | the external element that will be pinned
  parentElement | DOMElement | the host parent element that will be associated with the external element

- ###

  `enabled([element], [enabled]);`

  Used to get and set whether animations are enabled or not on the entire application or on an element and its children. This function can be called in four ways:

      // returns true or false
      $animate.enabled();

      // changes the enabled state for all animations
      $animate.enabled(false);
      $animate.enabled(true);

      // returns true or false if animations are enabled for an element
      $animate.enabled(element);

      // changes the enabled state for an element and its children
      $animate.enabled(element, true);
      $animate.enabled(element, false);

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  element (optional) | DOMElement | the element that will be considered for checking/setting the enabled state
  enabled (optional) | boolean | whether or not the animations will be enabled for the element

  #### Returns

  boolean | whether or not animations are enabled

- ###

  `cancel(animationRunner);`

  Cancels the provided animation and applies the end state of the animation. Note that this does not cancel the underlying operation, e.g. the setting of classes or adding the element to the DOM.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  animationRunner | animationRunner | An animation runner returned by an $animate function.

  #### Example

      angular.module('animationExample', ['ngAnimate']).component('cancelExample', {
        templateUrl: 'template.html',
        controller: function($element, $animate) {
          this.runner = null;

          this.addClass = function() {
            this.runner = $animate.addClass($element.find('div'), 'red');
            var ctrl = this;
            this.runner.finally(function() {
              ctrl.runner = null;
            });
          };

          this.removeClass = function() {
            this.runner = $animate.removeClass($element.find('div'), 'red');
            var ctrl = this;
            this.runner.finally(function() {
              ctrl.runner = null;
            });
          };

          this.cancel = function() {
            $animate.cancel(this.runner);
          };
        }
      });

      <p>
        <button id="add" ng-click="$ctrl.addClass()">Add</button>
        <button ng-click="$ctrl.removeClass()">Remove</button>
        <br>
        <button id="cancel" ng-click="$ctrl.cancel()" ng-disabled="!$ctrl.runner">Cancel</button>
        <br>
        <div id="target">CSS-Animated Text</div>
      </p>

      <cancel-example></cancel-example>

      .red-add, .red-remove {
        transition: all 4s cubic-bezier(0.250, 0.460, 0.450, 0.940);
      }

      .red,
      .red-add.red-add-active {
        color: #FF0000;
        font-size: 40px;
      }

      .red-remove.red-remove-active {
        font-size: 10px;
        color: black;
      }

- ###

  `enter(element, parent, [after], [options]);`

  Inserts the element into the DOM either after the `after` element (if provided) or as the first child within the `parent` element and then triggers an animation. A promise is returned that will be resolved during the next digest once the animation has completed.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  element | DOMElement | the element which will be inserted into the DOM
  parent | DOMElement | the parent element which will append the element as a child (so long as the after element is not present)
  after (optional) | DOMElement | the sibling element after which the element will be appended
  options (optional) | object | an optional collection of options/styles that will be applied to the element. The object can have the following properties: addClass - `{string}` - space-separated CSS classes to add to element from - `{Object}` - CSS properties & values at the beginning of animation. Must have matching `to` removeClass - `{string}` - space-separated CSS classes to remove from element to - `{Object}` - CSS properties & values at end of animation. Must have matching `from`

  #### Returns

  Runner | the animation runner

- ###

  `move(element, parent, [after], [options]);`

  Inserts (moves) the element into its new position in the DOM either after the `after` element (if provided) or as the first child within the `parent` element and then triggers an animation. A promise is returned that will be resolved during the next digest once the animation has completed.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  element | DOMElement | the element which will be moved into the new DOM position
  parent | DOMElement | the parent element which will append the element as a child (so long as the after element is not present)
  after (optional) | DOMElement | the sibling element after which the element will be appended
  options (optional) | object | an optional collection of options/styles that will be applied to the element. The object can have the following properties: addClass - `{string}` - space-separated CSS classes to add to element from - `{Object}` - CSS properties & values at the beginning of animation. Must have matching `to` removeClass - `{string}` - space-separated CSS classes to remove from element to - `{Object}` - CSS properties & values at end of animation. Must have matching `from`

  #### Returns

  Runner | the animation runner

- ###

  `leave(element, [options]);`

  Triggers an animation and then removes the element from the DOM. When the function is called a promise is returned that will be resolved during the next digest once the animation has completed.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  element | DOMElement | the element which will be removed from the DOM
  options (optional) | object | an optional collection of options/styles that will be applied to the element. The object can have the following properties: addClass - `{string}` - space-separated CSS classes to add to element from - `{Object}` - CSS properties & values at the beginning of animation. Must have matching `to` removeClass - `{string}` - space-separated CSS classes to remove from element to - `{Object}` - CSS properties & values at end of animation. Must have matching `from`

  #### Returns

  Runner | the animation runner

- ###

  `addClass(element, className, [options]);`

  Triggers an addClass animation surrounding the addition of the provided CSS class(es). Upon execution, the addClass operation will only be handled after the next digest and it will not trigger an animation if element already contains the CSS class or if the class is removed at a later step. Note that class-based animations are treated differently compared to structural animations (like enter, move and leave) since the CSS classes may be added/removed at different points depending if CSS or JavaScript animations are used.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  element | DOMElement | the element which the CSS classes will be applied to
  className | string | the CSS class(es) that will be added (multiple classes are separated via spaces)
  options (optional) | object | an optional collection of options/styles that will be applied to the element. The object can have the following properties: removeClass - `{string}` - space-separated CSS classes to remove from element from - `{Object}` - CSS properties & values at the beginning of animation. Must have matching `to` to - `{Object}` - CSS properties & values at end of animation. Must have matching `from`

  #### Returns

  Runner | animationRunner the animation runner

- ###

  `removeClass(element, className, [options]);`

  Triggers a removeClass animation surrounding the removal of the provided CSS class(es). Upon execution, the removeClass operation will only be handled after the next digest and it will not trigger an animation if element does not contain the CSS class or if the class is added at a later step. Note that class-based animations are treated differently compared to structural animations (like enter, move and leave) since the CSS classes may be added/removed at different points depending if CSS or JavaScript animations are used.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  element | DOMElement | the element which the CSS classes will be applied to
  className | string | the CSS class(es) that will be removed (multiple classes are separated via spaces)
  options (optional) | object | an optional collection of options/styles that will be applied to the element. The object can have the following properties: addClass - `{string}` - space-separated CSS classes to add to element from - `{Object}` - CSS properties & values at the beginning of animation. Must have matching `to` to - `{Object}` - CSS properties & values at end of animation. Must have matching `from`

  #### Returns

  Runner | the animation runner

- ###

  `setClass(element, add, remove, [options]);`

  Performs both the addition and removal of a CSS classes on an element and (during the process) triggers an animation surrounding the class addition/removal. Much like `$animate.addClass` and `$animate.removeClass`, `setClass` will only evaluate the classes being added/removed once a digest has passed. Note that class-based animations are treated differently compared to structural animations (like enter, move and leave) since the CSS classes may be added/removed at different points depending if CSS or JavaScript animations are used.

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  element | DOMElement | the element which the CSS classes will be applied to
  add | string | the CSS class(es) that will be added (multiple classes are separated via spaces)
  remove | string | the CSS class(es) that will be removed (multiple classes are separated via spaces)
  options (optional) | object | an optional collection of options/styles that will be applied to the element. The object can have the following properties: addClass - `{string}` - space-separated CSS classes to add to element removeClass - `{string}` - space-separated CSS classes to remove from element from - `{Object}` - CSS properties & values at the beginning of animation. Must have matching `to` to - `{Object}` - CSS properties & values at end of animation. Must have matching `from`

  #### Returns

  Runner | the animation runner

- ###

  `animate(element, from, to, [className], [options]);`

  Performs an inline animation on the element which applies the provided to and from CSS styles to the element. If any detected CSS transition, keyframe or JavaScript matches the provided className value, then the animation will take on the provided styles. For example, if a transition animation is set for the given className, then the provided `from` and `to` styles will be applied alongside the given transition. If the CSS style provided in `from` does not have a corresponding style in `to`, the style in `from` is applied immediately, and no animation is run. If a JavaScript animation is detected then the provided styles will be given in as function parameters into the `animate` method (or as part of the `options` parameter):

      ngModule.animation('.my-inline-animation', function() {
        return {
          animate : function(element, from, to, done, options) {
            //animation
            done();
          }
        }
      });

  #### Parameters

  Param | Type | Details
  --- | --- | ---
  element | DOMElement | the element which the CSS styles will be applied to
  from | object | the from (starting) CSS styles that will be applied to the element and across the animation.
  to | object | the to (destination) CSS styles that will be applied to the element and across the animation.
  className (optional) | string | an optional CSS class that will be applied to the element for the duration of the animation. If this value is left as empty then a CSS class of `ng-inline-animate` will be applied to the element. (Note that if no animation is detected then this value will not be applied to the element.)
  options (optional) | object | an optional collection of options/styles that will be applied to the element. The object can have the following properties: addClass - `{string}` - space-separated CSS classes to add to element from - `{Object}` - CSS properties & values at the beginning of animation. Must have matching `to` removeClass - `{string}` - space-separated CSS classes to remove from element to - `{Object}` - CSS properties & values at end of animation. Must have matching `from`

  #### Returns

  Runner | the animation runner