Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}]);
like image 622
Nicolas Del Valle Avatar asked Jun 10 '15 16:06

Nicolas Del Valle


People also ask

What are the types of dependencies injectable?

There are three types of dependency injection — constructor injection, method injection, and property injection.

How are dependency injected?

In software engineering, dependency injection is a design pattern in which an object or function receives other objects or functions that it depends on. A form of inversion of control, dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs.

What is dependency injection example?

Dependency injection (DI) is a technique widely used in programming and well suited to Android development. By following the principles of DI, you lay the groundwork for good app architecture. Implementing dependency injection provides you with the following advantages: Reusability of code.

What is dependency in dependency injection?

In object-oriented programming (OOP) software design, dependency injection (DI) is the process of supplying a resource that a given piece of code requires. The required resource, which is often a component of the application itself, is called a dependency.


2 Answers

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.

like image 50
scniro Avatar answered Sep 17 '22 19:09

scniro


Building on @salniro's answer, you don't need globals, or $window.

The dependencies are listed on the .requires property of angular.Module:

angular.module('myModule', [
    'ui.bootstrap',
    'ngRoute'
])
.controller('ctrl', function() {
    var app = angular.module('myModule');
    console.log(app.requires); // ["ui.bootstrap", "ngRoute"]
});

http://jsfiddle.net/8vtf6gar/1/

like image 26
Zach Lysobey Avatar answered Sep 20 '22 19:09

Zach Lysobey