Services and injection
A service is a class marked @Injectable(). This page covers the decorator's options and the three functions a service uses to reach its dependencies.
@Injectable
The decorator attaches a token to the class and records the class as that token's default provider. It does not create anything. Instances are built by a container, on first use.
Implementing a token
With token, the class becomes the default implementation of something declared elsewhere, usually an interface:
inject(MAILER) now returns an SmtpMailer, typed as Mailer. The decorator checks that the class matches the token's type. The class uses the token as its own, so container.get(MAILER) and container.get(SmtpMailer) return the same instance.
A token has one default. A second class declaring token: MAILER throws a DIError with code ProviderConflict when it is decorated. Register alternatives with plain @Injectable() and select one per container with provide().
The one exception is a class with the same name as the current default, outside production (NODE_ENV): it replaces the default without an error. That is what happens when a module is evaluated again during hot reload.
Constructor arguments
A service can take one constructor argument, described by a schema. The caller passes the argument when resolving, and the container validates it before constructing:
The container keeps one instance per distinct argument value. Tokens with a schema explains validation and instance identity; the rules are the same for a class and for a token.
When a class is resolved directly, the argument is typed as the constructor parameter, which is the schema's output, while the container validates it as input. For a schema with defaults or transforms the two differ, and a value that fits the type may be rejected. Use a token for such a schema: callers of a token pass the schema's input type.
inject
inject() returns the instance of a dependency. If it does not exist yet, it is built right there.
It accepts a class, a token, and a second argument for anything that takes one. The return type is inferred from the first argument.
inject(), optional() and asyncInject() only work while the container is constructing a class: in a field initialiser or in the constructor body. Called anywhere else, such as in a method, in a lifecycle hook or at module level, they throw. To resolve something later, inject the container itself, or the current scope for request-scoped services, and call get() on it:
Calls can be conditional, because dependencies are found by running the constructor:
Injecting a transient gives each call its own instance.
Store what inject() returns, but do not call methods on it inside the constructor. When the dependency is not ready yet, the call ends the constructor early and the container runs it again once the dependency is ready. Constructor replay explains why, and onServiceInit is the place for that code.
optional
optional() returns the instance, or null when nothing provides the token: no default provider, no provider on the container and no instance added to it.
It does not catch errors. If a provider exists and fails, the failure propagates exactly as it would through inject().
asyncInject
asyncInject() returns a promise of the instance. The dependency starts resolving only after the current constructor has returned.
Its one purpose is to break a cycle between two services on purpose. Circular dependencies shows when that is the right call. Everywhere else, inject() is simpler and gives you the instance directly.
Registering without decorators
registerInjectable() does what the decorator does, as a function call. Use it for classes you do not own or in code that avoids decorator syntax.
It takes the same options as @Injectable and returns the token.