Search code examples
javascriptangularjsangularjs-controllerangularjs-module

List dependencies injected


Is there a way to know what dependencies were injected into my Angular module?

angular.module('myModule', [
  'ui.bootstrap'
])
.controller('myController', [function () {
  // var dependencies = Magic.dependencies;
  // console.log(dependencies);
}]);

Solution

  • In your controller, if you inject $window, you can dig for the dependencies, specifically, there exists a .requires on your module. To do this, you can either declare your module as a global var so we can find it on our $window, in this case, let's call it app - or - you can bypass globals and $window and call angular.module('myModule').requires directly.

    • I've added ngRoute as well to prove the array of dependencies that will be discoverable.


    var app = angular.module('myModule',
    [
        'ui.bootstrap',
        'ngRoute'
    ]).controller('ctrl', ['$scope', '$window', function($scope, $window) {
        console.log($window.app.requires) // ["ui.bootstrap", "ngRoute"]
        console.log(angular.module('myModule').requires) // without global - $window not needed
    }]);
    

    JSFiddle Link - working example


    Note - If leveraging globals, you can simply call the window as such: window.app.requires - without injecting $window. However, see the AngularJS $window docs to understand why $window is preferred.