Skip to content

Commit b6b52e6

Browse files
committed
fix(element_injector): fixed element injector to inject view dependencies into its components
1 parent dd9b08c commit b6b52e6

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

modules/angular2/src/core/compiler/element_injector.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ export class ElementInjector extends TreeNode<ElementInjector> {
831831
directParent(): ElementInjector { return this._proto.distanceToParent < 2 ? this.parent : null; }
832832

833833
private _isComponentKey(key: Key) {
834-
return this._proto._firstBindingIsComponent && key.id === this._proto._keyId0;
834+
return this._proto._firstBindingIsComponent && isPresent(key) && key.id === this._proto._keyId0;
835835
}
836836

837837
private _isDynamicallyLoadedComponentKey(key: Key) {
@@ -1134,7 +1134,11 @@ export class ElementInjector extends TreeNode<ElementInjector> {
11341134
private _getByKey(key: Key, visibility: Visibility, optional: boolean, requestor: Key) {
11351135
var ei = this;
11361136

1137-
var currentVisibility = LIGHT_DOM;
1137+
var currentVisibility = this._isComponentKey(requestor) ?
1138+
LIGHT_DOM_AND_SHADOW_DOM : // component can access both shadow dom
1139+
// and light dom dependencies
1140+
LIGHT_DOM;
1141+
11381142
var depth = visibility.depth;
11391143

11401144
if (!visibility.shouldIncludeSelf()) {
@@ -1144,9 +1148,7 @@ export class ElementInjector extends TreeNode<ElementInjector> {
11441148
ei = ei._parent;
11451149
} else {
11461150
ei = ei._host;
1147-
if (!visibility.crossComponentBoundaries) {
1148-
currentVisibility = SHADOW_DOM;
1149-
}
1151+
currentVisibility = visibility.crossComponentBoundaries ? LIGHT_DOM : SHADOW_DOM;
11501152
}
11511153
}
11521154

@@ -1159,15 +1161,14 @@ export class ElementInjector extends TreeNode<ElementInjector> {
11591161

11601162
depth -= ei._proto.distanceToParent;
11611163

1164+
// we check only one mode with the SHADOW_DOM visibility
11621165
if (currentVisibility === SHADOW_DOM) break;
11631166

11641167
if (isPresent(ei._parent)) {
11651168
ei = ei._parent;
11661169
} else {
11671170
ei = ei._host;
1168-
if (!visibility.crossComponentBoundaries) {
1169-
currentVisibility = SHADOW_DOM;
1170-
}
1171+
currentVisibility = visibility.crossComponentBoundaries ? LIGHT_DOM : SHADOW_DOM;
11711172
}
11721173
}
11731174

modules/angular2/test/core/compiler/element_injector_spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,16 @@ export function main() {
528528
expect(inj.get('injectable2')).toEqual('injectable1-injectable2');
529529
});
530530

531+
it("should instantiate components that depends on viewInjector dependencies", function () {
532+
var inj = injector([
533+
DirectiveBinding.createFromType(NeedsService,
534+
new DummyDirective({viewInjector: [
535+
bind('service').toValue('service')
536+
]}))
537+
], null, true);
538+
expect(inj.get(NeedsService).service).toEqual('service');
539+
});
540+
531541
it("should instantiate directives that depend on app services", function () {
532542
var appInjector = Injector.resolveAndCreate([
533543
bind("service").toValue("service")

0 commit comments

Comments
 (0)