1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Angular - Injector & Module / Service

Posted at

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.

Documents

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.

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?