From ac8351b7bc66943c89aeaa06f4bd88cbed01fb0e Mon Sep 17 00:00:00 2001 From: vsavkin Date: Fri, 14 Nov 2014 09:20:32 -0800 Subject: [PATCH] feat(ElementInjector): change ElementInjector so @parent and @ancestor do not include self. --- modules/core/src/compiler/element_injector.js | 11 +++++++++++ modules/core/test/compiler/element_injector_spec.js | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/modules/core/src/compiler/element_injector.js b/modules/core/src/compiler/element_injector.js index d10f28275b..43978bf094 100644 --- a/modules/core/src/compiler/element_injector.js +++ b/modules/core/src/compiler/element_injector.js @@ -377,6 +377,12 @@ export class ElementInjector extends TreeNode { */ _getByKey(key:Key, depth:int, requestor:Key) { var ei = this; + + if (! this._shouldIncludeSelf(depth)) { + depth -= 1; + ei = ei._parent; + } + while (ei != null && depth >= 0) { var specObj = ei._getSpecialObjectByKeyId(key.id); if (specObj !== _undefined) return specObj; @@ -387,6 +393,7 @@ export class ElementInjector extends TreeNode { ei = ei._parent; depth -= 1; } + if (isPresent(this._host) && this._host._isComponentKey(key)) { return this._host.getComponent(); } else { @@ -400,6 +407,10 @@ export class ElementInjector extends TreeNode { } } + _shouldIncludeSelf(depth:int) { + return depth === 0; + } + _getSpecialObjectByKeyId(keyId:int) { var staticKeys = StaticKeys.instance(); if (keyId === staticKeys.viewId) return this._view; diff --git a/modules/core/test/compiler/element_injector_spec.js b/modules/core/test/compiler/element_injector_spec.js index a3f52ef4f6..a90b8d6c48 100644 --- a/modules/core/test/compiler/element_injector_spec.js +++ b/modules/core/test/compiler/element_injector_spec.js @@ -225,6 +225,12 @@ export function main() { expect(d.dependency).toBeAnInstanceOf(Directive); }); + it("should not return parent's directives on self", function () { + expect(() => { + injector([Directive, NeedDirectiveFromParent]); + }).toThrowError(); + }); + it("should get directives from ancestor", function () { var child = parentChildInjectors([Directive], [NeedDirectiveFromAncestor]);