From 6391c6362da8c90349ab0c25e52c6fc86612c2ca Mon Sep 17 00:00:00 2001 From: SkyZeroZx <73321943+SkyZeroZx@users.noreply.github.com> Date: Fri, 12 Dec 2025 13:03:58 -0500 Subject: [PATCH 1/2] doc: add section on asserting injection context in dependency injection --- docs/dependency-injection.md | 37 ++++++++++++++++++ .../version-19/dependency-injection.md | 39 +++++++++++++++++++ .../version-20/dependency-injection.md | 37 ++++++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/docs/dependency-injection.md b/docs/dependency-injection.md index ebda6da..2fbcd9f 100644 --- a/docs/dependency-injection.md +++ b/docs/dependency-injection.md @@ -80,4 +80,41 @@ export class UserDialog { } ``` +## Asserting injection context +When creating reusable helper functions that use `inject()`, you may want to ensure they are only called from an injection context. The `assertInInjectionContext()` function helps you enforce this constraint and provide clear error messages. + +**Consider** using `assertInInjectionContext()` in helper functions that use `inject()`. + +**Do** call helper functions from injection contexts. + +```ts title="❌ Without assertion - unclear error" +export function injectBody(): HTMLElement { + return inject(DOCUMENT).body; + // Error: NullInjectorError: No provider for DOCUMENT! +} +``` + +```ts title="✅ With assertion - clear error" +export function injectBody(): HTMLElement { + assertInInjectionContext(injectBody); + return inject(DOCUMENT).body; + // Error: NG0203: injectBody() can only be used within an injection context +} +``` + +:::info Why? +`assertInInjectionContext()` provides a clearer, more actionable error message that points to your helper function instead of the generic `inject()` call. This makes debugging easier for developers using your code. +::: + +Helper functions using `inject()` can be called from: + +- ✅ Constructor +- ✅ Field initializer +- ✅ Within `runInInjectionContext()` +- ❌ Event handler +- ❌ Lifecycle hook + +:::warning Error NG0203 +Calling `inject()` or `assertInInjectionContext()` outside an injection context throws [error NG0203](https://angular.dev/errors/NG0203). Make sure to call these functions only during construction or initialization phases. +::: diff --git a/versioned_docs/version-19/dependency-injection.md b/versioned_docs/version-19/dependency-injection.md index 136ccf2..61d1fcd 100644 --- a/versioned_docs/version-19/dependency-injection.md +++ b/versioned_docs/version-19/dependency-injection.md @@ -80,4 +80,43 @@ export class DialogComponent { } ``` +## Asserting injection context + +When creating reusable helper functions that use `inject()`, you may want to ensure they are only called from an injection context. The `assertInInjectionContext()` function helps you enforce this constraint and provide clear error messages. + +**Consider** using `assertInInjectionContext()` in helper functions that use `inject()`. + +**Do** call helper functions from injection contexts. + +```ts title="❌ Without assertion - unclear error" +export function injectBody(): HTMLElement { + return inject(DOCUMENT).body; + // Error: NullInjectorError: No provider for DOCUMENT! +} +``` + +```ts title="✅ With assertion - clear error" +export function injectBody(): HTMLElement { + assertInInjectionContext(injectBody); + return inject(DOCUMENT).body; + // Error: NG0203: injectBody() can only be used within an injection context +} +``` + +:::info Why? +`assertInInjectionContext()` provides a clearer, more actionable error message that points to your helper function instead of the generic `inject()` call. This makes debugging easier for developers using your code. +::: + +Helper functions using `inject()` can be called from: + +- ✅ Constructor +- ✅ Field initializer +- ✅ Within `runInInjectionContext()` +- ❌ Event handler +- ❌ Lifecycle hook + +:::warning Error NG0203 +Calling `inject()` or `assertInInjectionContext()` outside an injection context throws [error NG0203](https://angular.dev/errors/NG0203). Make sure to call these functions only during construction or initialization phases. +::: + diff --git a/versioned_docs/version-20/dependency-injection.md b/versioned_docs/version-20/dependency-injection.md index ebda6da..2fbcd9f 100644 --- a/versioned_docs/version-20/dependency-injection.md +++ b/versioned_docs/version-20/dependency-injection.md @@ -80,4 +80,41 @@ export class UserDialog { } ``` +## Asserting injection context +When creating reusable helper functions that use `inject()`, you may want to ensure they are only called from an injection context. The `assertInInjectionContext()` function helps you enforce this constraint and provide clear error messages. + +**Consider** using `assertInInjectionContext()` in helper functions that use `inject()`. + +**Do** call helper functions from injection contexts. + +```ts title="❌ Without assertion - unclear error" +export function injectBody(): HTMLElement { + return inject(DOCUMENT).body; + // Error: NullInjectorError: No provider for DOCUMENT! +} +``` + +```ts title="✅ With assertion - clear error" +export function injectBody(): HTMLElement { + assertInInjectionContext(injectBody); + return inject(DOCUMENT).body; + // Error: NG0203: injectBody() can only be used within an injection context +} +``` + +:::info Why? +`assertInInjectionContext()` provides a clearer, more actionable error message that points to your helper function instead of the generic `inject()` call. This makes debugging easier for developers using your code. +::: + +Helper functions using `inject()` can be called from: + +- ✅ Constructor +- ✅ Field initializer +- ✅ Within `runInInjectionContext()` +- ❌ Event handler +- ❌ Lifecycle hook + +:::warning Error NG0203 +Calling `inject()` or `assertInInjectionContext()` outside an injection context throws [error NG0203](https://angular.dev/errors/NG0203). Make sure to call these functions only during construction or initialization phases. +::: From bac1306d8e1601026738309be19bd96dd8df16ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Bou=C3=A9?= Date: Fri, 19 Dec 2025 14:11:07 +0100 Subject: [PATCH 2/2] doc: assert injection context minor updates --- docs/dependency-injection.md | 8 +++----- versioned_docs/version-19/dependency-injection.md | 8 +++----- versioned_docs/version-20/dependency-injection.md | 8 +++----- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/docs/dependency-injection.md b/docs/dependency-injection.md index 2fbcd9f..3af1c14 100644 --- a/docs/dependency-injection.md +++ b/docs/dependency-injection.md @@ -86,8 +86,6 @@ When creating reusable helper functions that use `inject()`, you may want to ens **Consider** using `assertInInjectionContext()` in helper functions that use `inject()`. -**Do** call helper functions from injection contexts. - ```ts title="❌ Without assertion - unclear error" export function injectBody(): HTMLElement { return inject(DOCUMENT).body; @@ -107,7 +105,7 @@ export function injectBody(): HTMLElement { `assertInInjectionContext()` provides a clearer, more actionable error message that points to your helper function instead of the generic `inject()` call. This makes debugging easier for developers using your code. ::: -Helper functions using `inject()` can be called from: +**Do** call helper functions from injection contexts. - ✅ Constructor - ✅ Field initializer @@ -115,6 +113,6 @@ Helper functions using `inject()` can be called from: - ❌ Event handler - ❌ Lifecycle hook -:::warning Error NG0203 -Calling `inject()` or `assertInInjectionContext()` outside an injection context throws [error NG0203](https://angular.dev/errors/NG0203). Make sure to call these functions only during construction or initialization phases. +:::info Why? +Calling `inject()` or `assertInInjectionContext()` outside an injection context throws [error NG0203](https://v21.angular.dev/errors/NG0203). Make sure to call these functions only during construction or initialization phases. ::: diff --git a/versioned_docs/version-19/dependency-injection.md b/versioned_docs/version-19/dependency-injection.md index 61d1fcd..cbeb556 100644 --- a/versioned_docs/version-19/dependency-injection.md +++ b/versioned_docs/version-19/dependency-injection.md @@ -86,8 +86,6 @@ When creating reusable helper functions that use `inject()`, you may want to ens **Consider** using `assertInInjectionContext()` in helper functions that use `inject()`. -**Do** call helper functions from injection contexts. - ```ts title="❌ Without assertion - unclear error" export function injectBody(): HTMLElement { return inject(DOCUMENT).body; @@ -107,7 +105,7 @@ export function injectBody(): HTMLElement { `assertInInjectionContext()` provides a clearer, more actionable error message that points to your helper function instead of the generic `inject()` call. This makes debugging easier for developers using your code. ::: -Helper functions using `inject()` can be called from: +**Do** call helper functions from injection contexts. - ✅ Constructor - ✅ Field initializer @@ -115,8 +113,8 @@ Helper functions using `inject()` can be called from: - ❌ Event handler - ❌ Lifecycle hook -:::warning Error NG0203 -Calling `inject()` or `assertInInjectionContext()` outside an injection context throws [error NG0203](https://angular.dev/errors/NG0203). Make sure to call these functions only during construction or initialization phases. +:::info Why? +Calling `inject()` or `assertInInjectionContext()` outside an injection context throws [error NG0203](https://v19.angular.dev/errors/NG0203). Make sure to call these functions only during construction or initialization phases. ::: diff --git a/versioned_docs/version-20/dependency-injection.md b/versioned_docs/version-20/dependency-injection.md index 2fbcd9f..a67aabe 100644 --- a/versioned_docs/version-20/dependency-injection.md +++ b/versioned_docs/version-20/dependency-injection.md @@ -86,8 +86,6 @@ When creating reusable helper functions that use `inject()`, you may want to ens **Consider** using `assertInInjectionContext()` in helper functions that use `inject()`. -**Do** call helper functions from injection contexts. - ```ts title="❌ Without assertion - unclear error" export function injectBody(): HTMLElement { return inject(DOCUMENT).body; @@ -107,7 +105,7 @@ export function injectBody(): HTMLElement { `assertInInjectionContext()` provides a clearer, more actionable error message that points to your helper function instead of the generic `inject()` call. This makes debugging easier for developers using your code. ::: -Helper functions using `inject()` can be called from: +**Do** call helper functions from injection contexts. - ✅ Constructor - ✅ Field initializer @@ -115,6 +113,6 @@ Helper functions using `inject()` can be called from: - ❌ Event handler - ❌ Lifecycle hook -:::warning Error NG0203 -Calling `inject()` or `assertInInjectionContext()` outside an injection context throws [error NG0203](https://angular.dev/errors/NG0203). Make sure to call these functions only during construction or initialization phases. +:::info Why? +Calling `inject()` or `assertInInjectionContext()` outside an injection context throws [error NG0203](https:/v20.angular.dev/errors/NG0203). Make sure to call these functions only during construction or initialization phases. :::