MCPcopy
hub / github.com/angular/angular / get

Method get

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

Source from the content-addressed store, hash-verified

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