Skip to content

Commit 249fbb3

Browse files
SkyZeroZxAndrewKushnir
authored andcommitted
docs: add example for assertInInjectionContext (angular#64325)
PR Close angular#64325
1 parent 8693959 commit 249fbb3

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

adev/src/content/guide/di/dependency-injection-context.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ This means that injectors can only work when code is executed in such a context.
55

66
The injection context is available in these situations:
77

8-
* During construction (via the `constructor`) of a class being instantiated by the DI system, such as an `@Injectable` or `@Component`.
9-
* In the initializer for fields of such classes.
10-
* In the factory function specified for `useFactory` of a `Provider` or an `@Injectable`.
11-
* In the `factory` function specified for an `InjectionToken`.
12-
* Within a stack frame that runs in an injection context.
8+
- During construction (via the `constructor`) of a class being instantiated by the DI system, such as an `@Injectable` or `@Component`.
9+
- In the initializer for fields of such classes.
10+
- In the factory function specified for `useFactory` of a `Provider` or an `@Injectable`.
11+
- In the `factory` function specified for an `InjectionToken`.
12+
- Within a stack frame that runs in an injection context.
1313

1414
Knowing when you are in an injection context will allow you to use the [`inject`](api/core/inject) function to inject instances.
1515

@@ -66,7 +66,32 @@ Note that `inject` will return an instance only if the injector can resolve the
6666

6767
## Asserts the context
6868

69-
Angular provides the `assertInInjectionContext` helper function to assert that the current context is an injection context.
69+
Angular provides the `assertInInjectionContext` helper function to assert that the current context is an injection context and throws a clear error if not. Pass a reference to the calling function so the error message points to the correct API entry point. This produces a clearer, more actionable message than the default generic injection error.
70+
71+
```ts
72+
import { ElementRef, assertInInjectionContext, inject } from '@angular/core';
73+
74+
export function injectNativeElement<T extends Element>(): T {
75+
assertInInjectionContext(injectNativeElement);
76+
return inject(ElementRef).nativeElement;
77+
}
78+
```
79+
80+
You can then call this helper **from an injection context** (constructor, field initializer, provider factory, or code executed via `runInInjectionContext`):
81+
82+
```ts
83+
import { Component, inject } from '@angular/core';
84+
import { injectNativeElement } from './dom-helpers';
85+
86+
@Component({ /**/ })
87+
export class PreviewCard {
88+
readonly hostEl = injectNativeElement<HTMLElement>(); // Field initializer runs in an injection context.
89+
90+
onAction() {
91+
const anotherRef = injectNativeElement<HTMLElement>(); // Fails: runs outside an injection context.
92+
}
93+
}
94+
```
7095

7196
## Using DI outside of a context
7297

packages/core/src/di/contextual.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export function isInInjectionContext(): boolean {
6868
*
6969
* @param debugFn a reference to the function making the assertion (used for the error message).
7070
*
71+
* @see [Asserts the context](guide/di/dependency-injection-context#asserts-the-context)
72+
*
7173
* @publicApi
7274
*/
7375
export function assertInInjectionContext(debugFn: Function): void {

0 commit comments

Comments
 (0)