Overview
-
Injector is a service locator. There's a single injector per Angular application.
-
Injector looks up an object by its name, and keeps an internal caches of all objects. Repeated calls to get the same object name result in the same instance.
-
If the object does not exit, injector will asks the instance factory to create a new one.
-
So, a module is the way to configure the injector's instance factory, known as a provider.
Injector
$injector
is used to retrive object instances as defined by provider, instantiate types, invoke methods, and load modules.
var $injector = angular.injector();
expect($injector.get('$injector')).toBe($injector);
expect($injector.invoke(function($injector){
return $injector;
}).toBe($injector);
Annotation Strategy
Javascript does not have annotations, annotations are needed for dependency injection. But some minified/obfuscated tool will change the name of arguments, so we need some ways to make valid annotations.
// inferred (only works is code not minified/obfuscated)
$inject.invoke(function(serviceA){});
// annotated
function explicit(serviceA){};
explicit.$inject = ['serviceA'];
$inject.invoke(explicit);
// inline
$inject.invoke(['serviceA', function(serviceA){}]);
Module
The angular.module
is a global place for creating and registering Angular modules.
All Modules (angular core or 3rd party) that should be available to an application must be registered using this mechanism.