fix(compiler): work well with forwardRef with useValue / useFactory

The new expression lowering lowers everything after `useValue` / `useFactory`
into a separate exported variable. If the value was a `forwardRef`, this
was passed to the runtime and resulted in errors.

This change unwraps `forwardRef`s during runtime again.

Note: we can’t unwrap the `forwardRef` into an exported variable
during compile time, as this would defeat the purpose of the
`forwardRef` in referring to something that can’t be referred to
at this position.
This commit is contained in:
Tobias Bosch
2017-09-24 12:11:36 -07:00
committed by Victor Berchet
parent e31a76ce24
commit 1dacae2c3c
5 changed files with 43 additions and 5 deletions

View File

@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {resolveForwardRef} from '../di/forward_ref';
import {Injector, THROW_IF_NOT_FOUND} from '../di/injector';
import {NgModuleRef} from '../linker/ng_module_factory';
@ -20,6 +21,10 @@ const NgModuleRefTokenKey = tokenKey(NgModuleRef);
export function moduleProvideDef(
flags: NodeFlags, token: any, value: any,
deps: ([DepFlags, any] | any)[]): NgModuleProviderDef {
// Need to resolve forwardRefs as e.g. for `useValue` we
// lowered the expression and then stopped evaluating it,
// i.e. also didn't unwrap it.
value = resolveForwardRef(value);
const depDefs = splitDepsDsl(deps);
return {
// will bet set by the module definition

View File

@ -7,7 +7,7 @@
*/
import {ChangeDetectorRef, SimpleChange, SimpleChanges, WrappedValue} from '../change_detection/change_detection';
import {Injector} from '../di';
import {Injector, resolveForwardRef} from '../di';
import {ElementRef} from '../linker/element_ref';
import {TemplateRef} from '../linker/template_ref';
import {ViewContainerRef} from '../linker/view_container_ref';
@ -77,6 +77,10 @@ export function _def(
if (!bindings) {
bindings = [];
}
// Need to resolve forwardRefs as e.g. for `useValue` we
// lowered the expression and then stopped evaluating it,
// i.e. also didn't unwrap it.
value = resolveForwardRef(value);
const depDefs = splitDepsDsl(deps);