diff --git a/aio/content/errors/NG0203.md b/aio/content/errors/NG0203.md index 241c3373faa..77c77c66c15 100644 --- a/aio/content/errors/NG0203.md +++ b/aio/content/errors/NG0203.md @@ -1,9 +1,10 @@ @name `inject()` must be called from an injection context @category runtime -@shortDescription `inject()` must be called from an injection context (a constructor, a factory function or a field initializer) +@shortDescription `inject()` must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `EnvironmentInjector#runInContext`. @description -You see this error when you try to use the `inject()` function outside of the allowed injection context. The injection context is available during the class creation and initialization. +You see this error when you try to use the `inject()` function outside of the allowed injection context. The injection context is available during the class creation and initialization. It is also available to functions +used with `EnvironmentInjector#runInContext`. In practice the `inject()` calls are allowed in a constructor, a constructor parameter and a field initializer: @@ -34,7 +35,7 @@ providers: [ ] ``` -Calls to the `inject()` function outside of the class creation context will result in error. Most notably, calls to `inject()` are disallowed after a class instance was created, in methods (including lifecycle hooks): +Calls to the `inject()` function outside of the class creation or `runInContext` will result in error. Most notably, calls to `inject()` are disallowed after a class instance was created, in methods (including lifecycle hooks): ```typescript @Component({ ... }) diff --git a/packages/core/src/di/injector_compatibility.ts b/packages/core/src/di/injector_compatibility.ts index 52764be3039..fee67983f05 100644 --- a/packages/core/src/di/injector_compatibility.ts +++ b/packages/core/src/di/injector_compatibility.ts @@ -57,7 +57,7 @@ export function injectInjectorOnly(token: ProviderToken, flags = InjectFla throw new RuntimeError( RuntimeErrorCode.MISSING_INJECTION_CONTEXT, ngDevMode && - `inject() must be called from an injection context (a constructor, a factory function or a field initializer)`); + `inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with \`EnvironmentInjector#runInContext\`.`); } else if (_currentInjector === null) { return injectRootLimpMode(token, undefined, flags); } else {