diff --git a/packages/core/src/render3/di.ts b/packages/core/src/render3/di.ts index 03078b8c52..5c2b8d6e59 100644 --- a/packages/core/src/render3/di.ts +++ b/packages/core/src/render3/di.ts @@ -326,10 +326,9 @@ export function injectAttributeImpl(tNode: TNode, attrNameToInject: string): str * @returns the value from the injector, `null` when not found, or `notFoundValue` if provided */ export function getOrCreateInjectable( - tNode: TElementNode | TContainerNode | TElementContainerNode | null, lView: LView, - token: Type| InjectionToken, flags: InjectFlags = InjectFlags.Default, - notFoundValue?: any): T|null { - if (tNode) { + tNode: TDirectiveHostNode | null, lView: LView, token: Type| InjectionToken, + flags: InjectFlags = InjectFlags.Default, notFoundValue?: any): T|null { + if (tNode !== null) { const bloomHash = bloomHashBitOrFactory(token); // If the ID stored here is a function, this is a special object like ElementRef or TemplateRef // so just call the factory function to create it. diff --git a/packages/core/src/render3/instructions/di.ts b/packages/core/src/render3/instructions/di.ts index 4d52f2ea59..fc97298d17 100644 --- a/packages/core/src/render3/instructions/di.ts +++ b/packages/core/src/render3/instructions/di.ts @@ -9,7 +9,8 @@ import {InjectFlags, InjectionToken, resolveForwardRef} from '../../di'; import {ɵɵinject} from '../../di/injector_compatibility'; import {Type} from '../../interface/type'; import {getOrCreateInjectable, injectAttributeImpl} from '../di'; -import {TContainerNode, TElementContainerNode, TElementNode} from '../interfaces/node'; +import {TDirectiveHostNode, TNodeType} from '../interfaces/node'; +import {assertNodeOfPossibleTypes} from '../node_assert'; import {getLView, getPreviousOrParentTNode} from '../state'; /** @@ -40,15 +41,15 @@ export function ɵɵdirectiveInject(token: Type| InjectionToken): T; export function ɵɵdirectiveInject(token: Type| InjectionToken, flags: InjectFlags): T; export function ɵɵdirectiveInject( token: Type| InjectionToken, flags = InjectFlags.Default): T|null { - token = resolveForwardRef(token); const lView = getLView(); // Fall back to inject() if view hasn't been created. This situation can happen in tests // if inject utilities are used before bootstrapping. if (lView == null) return ɵɵinject(token, flags); - + const tNode = getPreviousOrParentTNode(); + ngDevMode && assertNodeOfPossibleTypes( + tNode, TNodeType.Container, TNodeType.Element, TNodeType.ElementContainer); return getOrCreateInjectable( - getPreviousOrParentTNode() as TElementNode | TContainerNode | TElementContainerNode, lView, - token, flags); + tNode as TDirectiveHostNode, lView, resolveForwardRef(token), flags); } /**