MCPcopy Create free account
hub / github.com/angular/angular / injectInjectorOnly

Function injectInjectorOnly

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

Source from the content-addressed store, hash-verified

85 flags?: InternalInjectFlags,
86): T | null;
87export function injectInjectorOnly<T>(
88 token: ProviderToken<T>,
89 flags = InternalInjectFlags.Default,
90): T | null {
91 const currentInjector = getCurrentInjector();
92 if (currentInjector === undefined) {
93 throw new RuntimeError(
94 RuntimeErrorCode.MISSING_INJECTION_CONTEXT,
95 ngDevMode &&
96 `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\`.`,
97 );
98 } else if (currentInjector === null) {
99 return injectRootLimpMode(token, undefined, flags);
100 } else {
101 const options = convertToInjectOptions(flags);
102 // TODO: improve the typings here.
103 // `token` can be a multi: true provider definition, which is considered as a Token but not represented in the typings
104 const value = currentInjector.retrieve(token as PrimitivesInjectionToken<T>, options) as T;
105 ngDevMode && emitInjectEvent(token as Type<unknown>, value, flags);
106 if (isNotFound(value)) {
107 if (options.optional) {
108 return null;
109 }
110 throw value;
111 }
112 return value;
113 }
114}
115
116/**
117 * 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