Improve this Doc View Source $animate

  1. $animateProvider
  2. service in module ng

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
       }
    );
    

    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).
  • 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 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 `listenerFn` that is set
    // to listen for `enter` on the given `element` as well as its children
    $animate.off('enter', container, callback);
    

    Parameters

    Param Type Details
    event string

    the animation event (e.g. enter, leave, move, addClass, removeClass, etc...)

    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 Angular 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(animationPromise);

    Cancels the provided animation.

    Parameters

    Param Type Details
    animationPromise Promise

    The animation promise that is returned when an animation is started.

  • 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

    Returns

    Promise

    the animation callback promise

  • 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

    Returns

    Promise

    the animation callback promise

  • 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

    Returns

    Promise

    the animation callback promise

  • 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

    Returns

    Promise

    the animation callback promise

  • 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

    Returns

    Promise

    the animation callback promise

  • 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

    Returns

    Promise

    the animation callback promise

  • 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 appplied to the element.)

    options
    (optional)
    object

    an optional collection of options/styles that will be applied to the element

    Returns

    Promise

    the animation callback promise

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.4.14/docs/api/ng/service/$animate