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).

Usage

angular.injector(modules, [strictDi]);

Arguments

Param Type Details
modules Array.<string|Function>

A list of module functions or their aliases. See angular.module. The ng module must be explicitly added.

strictDi
(optional)
boolean

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

(default: false)

Returns

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) {
  $compile($document)($rootScope);
  $rootScope.$digest();
});

Sometimes you want to get access to the injector of a currently running Angular app from outside Angular. 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>');
$(document.body).append($div);

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

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.3.20/docs/api/ng/function/angular.injector