Horje

AngularJS - Dependency Injection
Start
  Dynamic Injections

There is also an option to dynamically request components. You can do it using the $injector service:

myModule.controller('myController', ['$injector', function($injector) {
    var myService = $injector.get('myService');
}]);

Note: while this method could be used to prevent the circular dependency issue that might break your app, it is not considered best practice to bypass the problem by using it. Circular dependency usually indicates there is a flaw in your application's architecture, and you should address that instead.

  Dynamically load AngularJS service in vanilla JavaScript

You can load AngularJS services in vanilla JavaScript using AngularJS injector() method. Every jqLite element retrieved calling angular.element() has a method injector() that can be used to retrieve the injector.

var service;
var serviceName = 'myService';

var ngAppElement = angular.element(document.querySelector('[ng-app],[data-ng-app]') || document);
var injector = ngAppElement.injector();

if(injector && injector.has(serviceNameToInject)) {
    service = injector.get(serviceNameToInject);
}

In the above example we try to retrieve the jqLite element containing the root of the AngularJS application (ngAppElement). To do that, we use angular.element() method, searching for a DOM element containing ng-app or data-ng-app attribute or, if it does not exists, we fall back to document element. We use ngAppElement to retrieve injector instance (with ngAppElement.injector()). The injector instance is used to check if the service to inject exists (with injector.has()) and then to load the service (with injector.get()) inside service variable.

  Dynamic Injections

There is also an option to dynamically request components. You can do it using the $injector service:

myModule.controller('myController', ['$injector', function($injector) {
    var myService = $injector.get('myService');
}]);

Note: while this method could be used to prevent the circular dependency issue that might break your app, it is not considered best practice to bypass the problem by using it. Circular dependency usually indicates there is a flaw in your application's architecture, and you should address that instead.

  $inject Property Annotation

Equivalently, we can use the $inject property annotation to achieve the same as above:

var MyController = function($scope) {
  // ...
}
MyController.$inject = ['$scope'];
myModule.controller('MyController', MyController);

Syntax

  • myApp.controller('MyController', function($scope) { ... }); // non-minified code
  • myApp.controller('MyController', ['$scope', function($scope) { ... }]); //support minification
  • function MyController(){} MyController.$inject = ['$scope']; myApp.controller('MyController', MyController); //$inject annotation
  • $injector.get('injectable'); //dynamic/runtime injection
  • Remarks

    Providers cannot be injected into run blocks.

    Services or Values cannot be injected into config blocks.

    Make sure to annotate your injections so your code will not break on minification.


    Read Article
    https://develop.horje.com/learn/1434/reference