Search code examples
javascriptangularjsanimationng-animate

Conditionally load ng-animate module?


We are using the Angular 1.4.9 ng-animate module. Everything works fine, but some of our teams are asking to a way to NOT load in based on a condition.

For example, on a particular platform or user agent, no ng-animate loads.

We already have a few ways to test for this (e.g.modernizr) and use these to conditionally load other .js and .css files, but since ng-animate is a module simply excluding its js file would throw an error.


Solution

  • If you bootstrap your application manually it's actually pretty simple.

    Keep your dependencies in a global variable:

    window.myApplicationMeta = {
      dependencies: [j]
    };
    

    Use this when you define your module:

    angular.module('myApp', applicationMeta.dependencies)
    

    Modify the dependencies before you bootstrap the application:

    angular.element(document).ready(function() {
    
      var condition = true;
    
      if (condition) applicationMeta.dependencies.push('ngAnimate');
    
      angular.bootstrap(document, ['myApp']); 
    });