* Returns a value from the closest embedded or 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 value to re
( tNode: TDirectiveHostNode, lView: LView, token: ProviderToken<T>, flags: InternalInjectFlags, notFoundValue?: any, )
| 972 | * @returns the value from the injector, `null` when not found, or `notFoundValue` if provided |
| 973 | */ |
| 974 | function lookupTokenUsingEmbeddedInjector<T>( |
| 975 | tNode: TDirectiveHostNode, |
| 976 | lView: LView, |
| 977 | token: ProviderToken<T>, |
| 978 | flags: InternalInjectFlags, |
| 979 | notFoundValue?: any, |
| 980 | ) { |
| 981 | let currentTNode: TDirectiveHostNode | null = tNode; |
| 982 | let currentLView: LView | null = lView; |
| 983 | const hostTElementNode: TNode | null = |
| 984 | flags & InternalInjectFlags.Host ? lView[DECLARATION_COMPONENT_VIEW][T_HOST] : null; |
| 985 | |
| 986 | // When an LView with an embedded view injector is inserted, it'll likely be interlaced with |
| 987 | // nodes who may have injectors (e.g. node injector -> embedded view injector -> node injector). |
| 988 | // Since the bloom filters for the node injectors have already been constructed and we don't |
| 989 | // have a way of extracting the records from an injector, the only way to maintain the correct |
| 990 | // hierarchy when resolving the value is to walk it node-by-node while attempting to resolve |
| 991 | // the token at each level. |
| 992 | while ( |
| 993 | currentTNode !== null && |
| 994 | currentLView !== null && |
| 995 | currentLView[FLAGS] & LViewFlags.HasEmbeddedViewInjector && |
| 996 | !isRootView(currentLView) |
| 997 | ) { |
| 998 | ngDevMode && assertTNodeForLView(currentTNode, currentLView); |
| 999 | |
| 1000 | // Note that this lookup on the node injector is using the `Self` flag, because |
| 1001 | // we don't want the node injector to look at any parent injectors since we |
| 1002 | // may hit the embedded view injector first. |
| 1003 | const nodeInjectorValue = lookupTokenUsingNodeInjector( |
| 1004 | currentTNode, |
| 1005 | currentLView, |
| 1006 | token, |
| 1007 | flags | InternalInjectFlags.Self, |
| 1008 | NOT_FOUND, |
| 1009 | ); |
| 1010 | if (nodeInjectorValue !== NOT_FOUND) { |
| 1011 | return nodeInjectorValue; |
| 1012 | } |
| 1013 | |
| 1014 | // The injector of the node we started from has been checked at this point, so everything |
| 1015 | // we look at from here on is a parent and must not be skipped by the `SkipSelf` flag. |
| 1016 | flags &= ~InternalInjectFlags.SkipSelf; |
| 1017 | |
| 1018 | // If @Host is set and we reached the host element node, we must not search any parent. |
| 1019 | if (flags & InternalInjectFlags.Host && currentTNode === hostTElementNode) { |
| 1020 | return notFoundValue; |
| 1021 | } |
| 1022 | |
| 1023 | // Has an explicit type due to a TS bug: https://github.com/microsoft/TypeScript/issues/33191 |
| 1024 | let parentTNode: TElementNode | TContainerNode | null = currentTNode.parent; |
| 1025 | |
| 1026 | // `TNode.parent` includes the parent within the current view only. If it doesn't exist, |
| 1027 | // it means that we've hit the view boundary and we need to go up to the next view. |
| 1028 | if (!parentTNode) { |
| 1029 | // Before we go to the next LView, check if the token exists on the current embedded injector. |
| 1030 | const embeddedViewInjector = currentLView[EMBEDDED_VIEW_INJECTOR]; |
| 1031 | if (embeddedViewInjector) { |
no test coverage detected