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

Method get

packages/core/src/di/r3_injector.ts:328–428  ·  view source on GitHub ↗
(
    token: ProviderToken<T>,
    notFoundValue: any = THROW_IF_NOT_FOUND,
    options?: InjectOptions,
  )

Source from the content-addressed store, hash-verified

326 // 3p-only-end
327
328 override get<T>(
329 token: ProviderToken<T>,
330 notFoundValue: any = THROW_IF_NOT_FOUND,
331 options?: InjectOptions,
332 ): T {
333 assertNotDestroyed(this);
334
335 if (Object.hasOwn(token, NG_ENV_ID)) {
336 return (token as any)[NG_ENV_ID](this);
337 }
338
339 const flags = convertToBitFlags(options) as InternalInjectFlags;
340
341 // Set the injection context.
342 let prevInjectContext: InjectorProfilerContext;
343 if (ngDevMode) {
344 prevInjectContext = setInjectorProfilerContext({injector: this, token: token as Type<T>});
345 }
346 const previousInjector = setCurrentInjector(this);
347 const previousInjectImplementation = setInjectImplementation(undefined);
348 try {
349 // Check for the SkipSelf flag.
350 if (!(flags & InternalInjectFlags.SkipSelf)) {
351 // SkipSelf isn't set, check if the record belongs to this injector.
352 let record: Record<T> | undefined | null = this.records.get(token);
353 if (record === undefined) {
354 // No record, but maybe the token is scoped to this injector. Look for an injectable
355 // def with a scope matching this injector.
356 const def = couldBeInjectableType(token) && getInjectableDef(token);
357 if (def && this.injectableDefInScope(def)) {
358 // Found an injectable def and it's scoped to this injector. Pretend as if it was here
359 // all along.
360
361 if (ngDevMode) {
362 runInInjectorProfilerContext(this, token as Type<T>, () => {
363 emitProviderConfiguredEvent(token as TypeProvider);
364 });
365 }
366
367 record = makeRecord(injectableDefOrInjectorDefFactory(token), NOT_YET);
368 } else {
369 record = null;
370 }
371 this.records.set(token, record);
372 }
373 // If a record was found, get the instance for it and return it.
374 if (record != null /* NOT null || undefined */) {
375 return this.hydrate(token, record, flags);
376 }
377 }
378
379 // Select the next injector based on the Self flag - if self is set, the next injector is
380 // the NullInjector, otherwise it's the parent.
381 const nextInjector = !(flags & InternalInjectFlags.Self) ? this.parent : getNullInjector();
382 // Set the notFoundValue based on the Optional flag - if optional is set and notFoundValue
383 // is undefined, the value is null, otherwise it's the notFoundValue.
384 notFoundValue =
385 flags & InternalInjectFlags.Optional && notFoundValue === THROW_IF_NOT_FOUND

Callers 2

constructorMethod · 0.95

Calls 15

injectableDefInScopeMethod · 0.95
hydrateMethod · 0.95
convertToBitFlagsFunction · 0.90
setInjectImplementationFunction · 0.90
getInjectableDefFunction · 0.90
getRuntimeErrorCodeFunction · 0.90
augmentRuntimeErrorFunction · 0.90
assertNotDestroyedFunction · 0.85

Tested by

no test coverage detected