Sub topic 12
Changing the lookup with optional, self, skipSelf and host
This chapter assumes you know how inject() works, and that you know the difference between element injectors and environment injectors. It builds on the default lookup order and shows how to change it.
The default lookup
By default, inject(Token) starts at the element injector of the requesting component or directive. It then walks up the element injectors of the ancestors. After that it checks the environment injectors (route, root, platform). If nothing provides the token, Angular throws NullInjectorError.
Four flags in the options object of inject() change that walk.
| Option | Effect |
|---|---|
| `optional` | Returns `null` instead of throwing when nothing is found. |
| `self` | Looks only at the requesting element's own injector. |
| `skipSelf` | Starts at the parent injector, ignoring the requesting element's own providers. |
| `host` | Stops the upward walk at the host element of the current component. |
The flags can be combined. self and skipSelf contradict each other, so don't use them together.
Example
import { Component, inject } from '@angular/core';
import { ThemeService } from './theme.service';
@Component({
selector: 'app-themed-panel',
standalone: true,
providers: [ThemeService],
template: `<ng-content />`,
})
export class ThemedPanelComponent {
private readonly ownTheme = inject(ThemeService, { self: true });
private readonly parentTheme = inject(ThemeService, { skipSelf: true, optional: true });
}The component lists ThemeService in its own providers, so self: true finds that instance. The second call skips the component's own injector and starts at the parent. If no ancestor or environment injector provides ThemeService, parentTheme is null instead of an error.
This skipSelf plus optional pairing is the usual way to let a nested component find an outer component of the same kind. A tree node is a typical case, because the outermost node has no parent to find.
Using host
host: true stops the lookup at the host element of the current component. Providers declared in a component's viewProviders stay visible to its own view. Providers from outside that boundary are not consulted, and the environment injectors are skipped too. Use it when a child should only accept a dependency from the component that directly hosts it, and never from something further up.
Choosing an option
- Use
optionalwhen the dependency is a real feature toggle. Check fornullbefore you use it. - Use
selfwhen you need to confirm that this element declared its own provider. - Use
skipSelfwhen the element provides a token and also wants the outer version of it. - Use
hostwhen the dependency must come from the immediate host and not from further up.
Exercise
A component declares providers: [LoggerService]. It calls inject(LoggerService, { skipSelf: true }), and no ancestor or environment injector provides LoggerService. What happens, and how do you change the call so the component still works?
Show answer
Angular throws NullInjectorError. skipSelf ignores the component's own provider, and nothing above it provides the token. Add optional: true so the call returns null, and handle that case in the code. Alternatively, provide LoggerService in an ancestor or at the root.