Improve this Doc View Source angular.injector

From Get docs
< Function components in ngAngularjs/docs/1.8/api/ng/function/angular.injector

Improve this Doc View Source angular.injector

  1. function in module ng


Creates an injector object that can be used for retrieving services as well as for dependency injection (see dependency injection).


angular.injector(modules, [strictDi]);


Param Type Details
modules Function> A list of module functions or their aliases. See angular.module. The ng module must be explicitly added.




Whether the injector should be in strict mode, which disallows argument name annotation inference.

(default: false)


injector Injector object. See $injector.


Typical usage

// create an injector
var $injector = angular.injector(['ng']);

// use the injector to kick off your application
// use the type inference to auto inject arguments, or use implicit injection
$injector.invoke(function($rootScope, $compile, $document) {

Sometimes you want to get access to the injector of a currently running AngularJS app from outside AngularJS. Perhaps, you want to inject and compile some markup after the application has been bootstrapped. You can do this using the extra injector() added to JQuery/jqLite elements. See angular.element.

This is fairly rare but could be the case if a third party library is injecting the markup.

In the following example a new block of HTML containing a ng-controller directive is added to the end of the document body by JQuery. We then compile and link it into the current AngularJS scope.

var $div = $('<div ng-controller="MyCtrl">{{content.label}}</div>');

angular.element(document).injector().invoke(function($compile) {
  var scope = angular.element($div).scope();

© 2010–2020 Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.