diff --git a/modules/angular2/src/core/compiler/element_injector.ts b/modules/angular2/src/core/compiler/element_injector.ts index e63138c508..a2932e8234 100644 --- a/modules/angular2/src/core/compiler/element_injector.ts +++ b/modules/angular2/src/core/compiler/element_injector.ts @@ -589,8 +589,10 @@ export class ElementInjector extends TreeNode implements Depend var key: Key = dep.key; if (!(dep instanceof DirectiveDependency)) return undefinedValue; + if (!(binding instanceof DirectiveBinding)) return undefinedValue; var dirDep = dep; + var dirBin = binding; var staticKeys = StaticKeys.instance(); @@ -601,8 +603,12 @@ export class ElementInjector extends TreeNode implements Depend if (isPresent(dirDep.queryDecorator)) return this._findQuery(dirDep.queryDecorator).list; if (dirDep.key.id === StaticKeys.instance().changeDetectorRefId) { - var componentView = this._preBuiltObjects.view.componentChildViews[this._proto.index]; - return componentView.changeDetector.ref; + if (dirBin.metadata.type === DirectiveMetadata.COMPONENT_TYPE) { + var componentView = this._preBuiltObjects.view.componentChildViews[this._proto.index]; + return componentView.changeDetector.ref; + } else { + return this._preBuiltObjects.view.changeDetector.ref; + } } if (dirDep.key.id === StaticKeys.instance().elementRefId) { diff --git a/modules/angular2/test/core/compiler/element_injector_spec.ts b/modules/angular2/test/core/compiler/element_injector_spec.ts index 4b4d0047a7..017b89a685 100644 --- a/modules/angular2/test/core/compiler/element_injector_spec.ts +++ b/modules/angular2/test/core/compiler/element_injector_spec.ts @@ -209,7 +209,13 @@ class OptionallyInjectsProtoViewRef { } @Injectable() -class NeedsChangeDetectorRef { +class DirectiveNeedsChangeDetectorRef { + changeDetectorRef; + constructor(cdr: ChangeDetectorRef) { this.changeDetectorRef = cdr; } +} + +@Injectable() +class ComponentNeedsChangeDetectorRef { changeDetectorRef; constructor(cdr: ChangeDetectorRef) { this.changeDetectorRef = cdr; } } @@ -944,16 +950,28 @@ export function main() { expect(inj.get(NeedsElementRef).elementRef).toBe(defaultPreBuiltObjects.view.elementRefs[0]); }); - it('should inject ChangeDetectorRef', () => { + it("should inject ChangeDetectorRef of the component's view into the component", () => { var cd = new DynamicChangeDetector(null, null, null, [], []); var view = new DummyView(); var childView = new DummyView(); childView.changeDetector = cd; view.componentChildViews = [childView]; - var inj = injector(ListWrapper.concat([NeedsChangeDetectorRef], extraBindings), null, false, + var binding = DirectiveBinding.createFromType(ComponentNeedsChangeDetectorRef, new dirAnn.Component()); + var inj = injector(ListWrapper.concat([binding], extraBindings), null, true, new PreBuiltObjects(null, view, null)); - expect(inj.get(NeedsChangeDetectorRef).changeDetectorRef).toBe(cd.ref); + expect(inj.get(ComponentNeedsChangeDetectorRef).changeDetectorRef).toBe(cd.ref); + }); + + it("should inject ChangeDetectorRef of the containing component into directives", () => { + var cd = new DynamicChangeDetector(null, null, null, [], []); + var view = new DummyView(); + view.changeDetector =cd; + var binding = DirectiveBinding.createFromType(DirectiveNeedsChangeDetectorRef, new dirAnn.Directive()); + var inj = injector(ListWrapper.concat([binding], extraBindings), null, false, + new PreBuiltObjects(null, view, null)); + + expect(inj.get(DirectiveNeedsChangeDetectorRef).changeDetectorRef).toBe(cd.ref); }); it('should inject ViewContainerRef', () => {