diff --git a/modules/angular2/src/core/compiler/element_injector.ts b/modules/angular2/src/core/compiler/element_injector.ts index 0e04f8a16a..b3e6003440 100644 --- a/modules/angular2/src/core/compiler/element_injector.ts +++ b/modules/angular2/src/core/compiler/element_injector.ts @@ -831,7 +831,7 @@ export class ElementInjector extends TreeNode { directParent(): ElementInjector { return this._proto.distanceToParent < 2 ? this.parent : null; } private _isComponentKey(key: Key) { - return this._proto._firstBindingIsComponent && key.id === this._proto._keyId0; + return this._proto._firstBindingIsComponent && isPresent(key) && key.id === this._proto._keyId0; } private _isDynamicallyLoadedComponentKey(key: Key) { @@ -1134,7 +1134,11 @@ export class ElementInjector extends TreeNode { private _getByKey(key: Key, visibility: Visibility, optional: boolean, requestor: Key) { var ei = this; - var currentVisibility = LIGHT_DOM; + var currentVisibility = this._isComponentKey(requestor) ? + LIGHT_DOM_AND_SHADOW_DOM : // component can access both shadow dom + // and light dom dependencies + LIGHT_DOM; + var depth = visibility.depth; if (!visibility.shouldIncludeSelf()) { @@ -1144,9 +1148,7 @@ export class ElementInjector extends TreeNode { ei = ei._parent; } else { ei = ei._host; - if (!visibility.crossComponentBoundaries) { - currentVisibility = SHADOW_DOM; - } + currentVisibility = visibility.crossComponentBoundaries ? LIGHT_DOM : SHADOW_DOM; } } @@ -1159,15 +1161,14 @@ export class ElementInjector extends TreeNode { depth -= ei._proto.distanceToParent; + // we check only one mode with the SHADOW_DOM visibility if (currentVisibility === SHADOW_DOM) break; if (isPresent(ei._parent)) { ei = ei._parent; } else { ei = ei._host; - if (!visibility.crossComponentBoundaries) { - currentVisibility = SHADOW_DOM; - } + currentVisibility = visibility.crossComponentBoundaries ? LIGHT_DOM : SHADOW_DOM; } } diff --git a/modules/angular2/test/core/compiler/element_injector_spec.js b/modules/angular2/test/core/compiler/element_injector_spec.js index 2fb2ebfc08..f1d7c0189a 100644 --- a/modules/angular2/test/core/compiler/element_injector_spec.js +++ b/modules/angular2/test/core/compiler/element_injector_spec.js @@ -528,6 +528,16 @@ export function main() { expect(inj.get('injectable2')).toEqual('injectable1-injectable2'); }); + it("should instantiate components that depends on viewInjector dependencies", function () { + var inj = injector([ + DirectiveBinding.createFromType(NeedsService, + new DummyDirective({viewInjector: [ + bind('service').toValue('service') + ]})) + ], null, true); + expect(inj.get(NeedsService).service).toEqual('service'); + }); + it("should instantiate directives that depend on app services", function () { var appInjector = Injector.resolveAndCreate([ bind("service").toValue("service")