MCPcopy Index your code
hub / github.com/angular/angular / injectInjectorOnly

Function injectInjectorOnly

packages/core/src/di/injector_compatibility.ts:93–120  ·  view source on GitHub ↗
(
  token: ProviderToken<T>,
  flags = InternalInjectFlags.Default,
)

Source from the content-addressed store, hash-verified

91 flags?: InternalInjectFlags,
92): T | null;
93export function injectInjectorOnly<T>(
94 token: ProviderToken<T>,
95 flags = InternalInjectFlags.Default,
96): T | null {
97 const currentInjector = getCurrentInjector();
98 if (currentInjector === undefined) {
99 throw new RuntimeError(
100 RuntimeErrorCode.MISSING_INJECTION_CONTEXT,
101 ngDevMode &&
102 `The \`${stringify(token)}\` token injection failed. \`inject()\` function must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with \`runInInjectionContext\`.`,
103 );
104 } else if (currentInjector === null) {
105 return injectRootLimpMode(token, undefined, flags);
106 } else {
107 const options = convertToInjectOptions(flags);
108 // TODO: improve the typings here.
109 // `token` can be a multi: true provider definition, which is considered as a Token but not represented in the typings
110 const value = currentInjector.retrieve(token as PrimitivesInjectionToken<T>, options) as T;
111 ngDevMode && emitInjectEvent(token as Type<unknown>, value, flags);
112 if (isNotFound(value)) {
113 if (options.optional) {
114 return null;
115 }
116 throw value;
117 }
118 return value;
119 }
120}
121
122/**
123 * Generated instruction: injects a token from the currently active injector.

Callers

nothing calls this directly

Calls 7

getCurrentInjectorFunction · 0.90
stringifyFunction · 0.90
injectRootLimpModeFunction · 0.90
emitInjectEventFunction · 0.90
isNotFoundFunction · 0.90
convertToInjectOptionsFunction · 0.85
retrieveMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…