|
9 | 9 | // We are temporarily importing the existing viewEngine_from core so we can be sure we are |
10 | 10 | // correctly implementing its interfaces for backwards compatibility. |
11 | 11 | import {ChangeDetectorRef as viewEngine_ChangeDetectorRef} from '../change_detection/change_detector_ref'; |
12 | | -import {InjectFlags, Injector} from '../di/injector'; |
| 12 | +import {InjectFlags, Injector, inject, setCurrentInjector} from '../di/injector'; |
13 | 13 | import {ComponentFactory as viewEngine_ComponentFactory, ComponentRef as viewEngine_ComponentRef} from '../linker/component_factory'; |
14 | 14 | import {ElementRef as viewEngine_ElementRef} from '../linker/element_ref'; |
15 | 15 | import {NgModuleRef as viewEngine_NgModuleRef} from '../linker/ng_module_factory'; |
@@ -125,24 +125,13 @@ export function getOrCreateNodeInjectorForNode(node: LElementNode | LContainerNo |
125 | 125 | cbf5: parentInjector == null ? 0 : parentInjector.cbf5 | parentInjector.bf5, |
126 | 126 | cbf6: parentInjector == null ? 0 : parentInjector.cbf6 | parentInjector.bf6, |
127 | 127 | cbf7: parentInjector == null ? 0 : parentInjector.cbf7 | parentInjector.bf7, |
128 | | - injector: null, |
129 | 128 | templateRef: null, |
130 | 129 | viewContainerRef: null, |
131 | 130 | elementRef: null, |
132 | 131 | changeDetectorRef: null |
133 | 132 | }; |
134 | 133 | } |
135 | 134 |
|
136 | | -/** |
137 | | - * Constructs an injection error with the given text and token. |
138 | | - * |
139 | | - * @param text The text of the error |
140 | | - * @param token The token associated with the error |
141 | | - * @returns The error that was created |
142 | | - */ |
143 | | -function createInjectionError(text: string, token: any) { |
144 | | - return new Error(`ElementInjector: ${text} [${stringify(token)}]`); |
145 | | -} |
146 | 135 |
|
147 | 136 | /** |
148 | 137 | * Makes a directive public to the DI system by adding it to an injector's bloom filter. |
@@ -188,14 +177,10 @@ export function diPublic(def: DirectiveDef<any>): void { |
188 | 177 | * @param flags Injection flags (e.g. CheckParent) |
189 | 178 | * @returns The instance found |
190 | 179 | */ |
191 | | -export function directiveInject<T>( |
192 | | - token: Type<T>, notFoundValue?: undefined, flags?: InjectFlags): T; |
193 | | -export function directiveInject<T>(token: Type<T>, notFoundValue: T, flags?: InjectFlags): T; |
194 | | -export function directiveInject<T>(token: Type<T>, notFoundValue: null, flags?: InjectFlags): T| |
195 | | - null; |
196 | | -export function directiveInject<T>( |
197 | | - token: Type<T>, notFoundValue?: T | null, flags = InjectFlags.Default): T|null { |
198 | | - return getOrCreateInjectable<T>(getOrCreateNodeInjector(), token, flags, notFoundValue); |
| 180 | +export function directiveInject<T>(token: Type<T>): T; |
| 181 | +export function directiveInject<T>(token: Type<T>, flags?: InjectFlags): T|null; |
| 182 | +export function directiveInject<T>(token: Type<T>, flags = InjectFlags.Default): T|null { |
| 183 | + return getOrCreateInjectable<T>(getOrCreateNodeInjector(), token, flags); |
199 | 184 | } |
200 | 185 |
|
201 | 186 | /** |
@@ -344,21 +329,20 @@ function getClosestComponentAncestor(node: LViewNode | LElementNode): LElementNo |
344 | 329 | * @param flags Injection flags (e.g. CheckParent) |
345 | 330 | * @returns The instance found |
346 | 331 | */ |
347 | | -export function getOrCreateInjectable<T>( |
348 | | - di: LInjector, token: Type<T>, flags?: InjectFlags, defaultValue?: T | null): T|null { |
| 332 | +export function getOrCreateInjectable<T>(di: LInjector, token: Type<T>, flags?: InjectFlags): T| |
| 333 | + null { |
349 | 334 | const bloomHash = bloomHashBit(token); |
350 | 335 |
|
351 | 336 | // If the token has a bloom hash, then it is a directive that is public to the injection system |
352 | 337 | // (diPublic). If there is no hash, fall back to the module injector. |
353 | 338 | if (bloomHash === null) { |
354 | | - const moduleInjector = di.injector; |
355 | | - if (!moduleInjector) { |
356 | | - if (defaultValue != null) { |
357 | | - return defaultValue; |
358 | | - } |
359 | | - throw createInjectionError('NotFound', token); |
| 339 | + const moduleInjector = getPreviousOrParentNode().view.injector; |
| 340 | + const formerInjector = setCurrentInjector(moduleInjector); |
| 341 | + try { |
| 342 | + return inject(token, flags); |
| 343 | + } finally { |
| 344 | + setCurrentInjector(formerInjector); |
360 | 345 | } |
361 | | - moduleInjector.get(token); |
362 | 346 | } else { |
363 | 347 | let injector: LInjector|null = di; |
364 | 348 |
|
@@ -409,7 +393,7 @@ export function getOrCreateInjectable<T>( |
409 | 393 |
|
410 | 394 | // No directive was found for the given token. |
411 | 395 | // TODO: implement optional, check-self, and check-parent. |
412 | | - throw createInjectionError('Not found', token); |
| 396 | + throw new Error('Implement'); |
413 | 397 | } |
414 | 398 |
|
415 | 399 | function searchMatchesQueuedForCreation<T>(node: LNode, token: any): T|null { |
|
0 commit comments