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

Function lookupTokenUsingNodeInjector

packages/core/src/render3/di.ts:507–626  ·  view source on GitHub ↗

* Returns the value associated to the given token from the node injector. * * @param tNode The Node where the search for the injector should start * @param lView The `LView` that contains the `tNode` * @param token The token to look for * @param flags Injection flags * @param notFoundValue The

(
  tNode: TDirectiveHostNode,
  lView: LView,
  token: ProviderToken<T>,
  flags: InternalInjectFlags,
  notFoundValue?: any,
)

Source from the content-addressed store, hash-verified

505 * @returns the value from the injector, `null` when not found, or `notFoundValue` if provided
506 */
507function lookupTokenUsingNodeInjector<T>(
508 tNode: TDirectiveHostNode,
509 lView: LView,
510 token: ProviderToken<T>,
511 flags: InternalInjectFlags,
512 notFoundValue?: any,
513) {
514 const bloomHash = bloomHashBitOrFactory(token);
515 // If the ID stored here is a function, this is a special object like ElementRef or TemplateRef
516 // so just call the factory function to create it.
517 if (typeof bloomHash === 'function') {
518 if (!enterDI(lView, tNode, flags)) {
519 // Failed to enter DI, try module injector instead. If a token is injected with the @Host
520 // flag, the module injector is not searched for that token in Ivy.
521 return flags & InternalInjectFlags.Host
522 ? notFoundValueOrThrow<T>(notFoundValue, token, flags)
523 : lookupTokenUsingModuleInjector<T>(lView, token, flags, notFoundValue);
524 }
525 try {
526 let value: unknown;
527
528 if (ngDevMode) {
529 runInInjectorProfilerContext(
530 new NodeInjector(getCurrentTNode() as TElementNode, getLView()),
531 token as Type<T>,
532 () => {
533 emitInjectorToCreateInstanceEvent(token);
534 value = bloomHash(flags);
535 emitInstanceCreatedByInjectorEvent(value);
536 },
537 );
538 } else {
539 value = bloomHash(flags);
540 }
541
542 if (value == null && !(flags & InternalInjectFlags.Optional)) {
543 throwProviderNotFoundError(token);
544 } else {
545 return value;
546 }
547 } finally {
548 leaveDI();
549 }
550 } else if (typeof bloomHash === 'number') {
551 // A reference to the previous injector TView that was found while climbing the element
552 // injector tree. This is used to know if viewProviders can be accessed on the current
553 // injector.
554 let previousTView: TView | null = null;
555 let injectorIndex = getInjectorIndex(tNode, lView);
556 let parentLocation = NO_PARENT_INJECTOR;
557 let hostTElementNode: TNode | null =
558 flags & InternalInjectFlags.Host ? lView[DECLARATION_COMPONENT_VIEW][T_HOST] : null;
559
560 // If we should skip this injector, or if there is no injector on this node, start by
561 // searching the parent injector.
562 if (injectorIndex === -1 || flags & InternalInjectFlags.SkipSelf) {
563 parentLocation =
564 injectorIndex === -1

Callers 2

getOrCreateInjectableFunction · 0.85

Calls 15

enterDIFunction · 0.90
getCurrentTNodeFunction · 0.90
getLViewFunction · 0.90
getParentInjectorIndexFunction · 0.90
getParentInjectorViewFunction · 0.90
assertNodeInjectorFunction · 0.90
assertTNodeForLViewFunction · 0.90
bloomHashBitOrFactoryFunction · 0.85

Tested by

no test coverage detected