diff --git a/modules/angular2/src/core/annotations_impl/visibility.ts b/modules/angular2/src/core/annotations_impl/visibility.ts index d570fb3149..9d8e1cb298 100644 --- a/modules/angular2/src/core/annotations_impl/visibility.ts +++ b/modules/angular2/src/core/annotations_impl/visibility.ts @@ -1,11 +1,14 @@ -import {CONST, CONST_EXPR} from 'angular2/src/facade/lang'; +import {CONST, CONST_EXPR, isBlank} from 'angular2/src/facade/lang'; import {DependencyAnnotation} from 'angular2/src/di/annotations_impl'; @CONST() export class Visibility extends DependencyAnnotation { - constructor(public depth: number, public crossComponentBoundaries: boolean) { super(); } + constructor(public depth: number, public crossComponentBoundaries: boolean, + public _includeSelf: boolean) { + super(); + } - shouldIncludeSelf(): boolean { return this.depth === 0; } + get includeSelf(): boolean { return isBlank(this._includeSelf) ? false : this._includeSelf; } } /** @@ -47,7 +50,7 @@ export class Visibility extends DependencyAnnotation { */ @CONST() export class Self extends Visibility { - constructor() { super(0, false); } + constructor() { super(0, false, true); } } // make constants after switching to ts2dart @@ -98,7 +101,7 @@ export var self = new Self(); */ @CONST() export class Parent extends Visibility { - constructor() { super(1, false); } + constructor({self}: {self?: boolean} = {}) { super(1, false, self); } } /** @@ -159,7 +162,7 @@ export class Parent extends Visibility { */ @CONST() export class Ancestor extends Visibility { - constructor() { super(999999, false); } + constructor({self}: {self?: boolean} = {}) { super(999999, false, self); } } /** @@ -198,5 +201,5 @@ export class Ancestor extends Visibility { */ @CONST() export class Unbounded extends Visibility { - constructor() { super(999999, true); } + constructor({self}: {self?: boolean} = {}) { super(999999, true, self); } } diff --git a/modules/angular2/src/core/compiler/element_injector.ts b/modules/angular2/src/core/compiler/element_injector.ts index 0acf9fd699..637dc09ecb 100644 --- a/modules/angular2/src/core/compiler/element_injector.ts +++ b/modules/angular2/src/core/compiler/element_injector.ts @@ -1082,7 +1082,7 @@ export class ElementInjector extends TreeNode { var depth = visibility.depth; - if (!visibility.shouldIncludeSelf()) { + if (!visibility.includeSelf) { depth -= ei._proto.distanceToParent; if (isPresent(ei._parent)) { diff --git a/modules/angular2/test/core/compiler/element_injector_spec.ts b/modules/angular2/test/core/compiler/element_injector_spec.ts index ce45074155..b062dd0341 100644 --- a/modules/angular2/test/core/compiler/element_injector_spec.ts +++ b/modules/angular2/test/core/compiler/element_injector_spec.ts @@ -103,6 +103,12 @@ class NeedsDirectiveFromParent { constructor(@Parent() dependency: SimpleDirective) { this.dependency = dependency; } } +@Injectable() +class NeedsDirectiveFromParentOrSelf { + dependency: SimpleDirective; + constructor(@Parent({self:true}) dependency: SimpleDirective) { this.dependency = dependency; } +} + @Injectable() class NeedsDirectiveFromAncestor { dependency: SimpleDirective; @@ -640,12 +646,21 @@ export function main() { expect(d.dependency).toBeAnInstanceOf(SimpleDirective); }); - it("should not return parent's directives on self", () => { + it("should not return parent's directives on self by default", () => { expect(() => { injector(ListWrapper.concat([SimpleDirective, NeedsDirectiveFromParent], extraBindings)); }).toThrowError(containsRegexp(`No provider for ${stringify(SimpleDirective) }`)); }); + it("should return parent's directives on self when explicitly specified", () => { + var inj = injector(ListWrapper.concat([SimpleDirective, NeedsDirectiveFromParentOrSelf], extraBindings)); + + var d = inj.get(NeedsDirectiveFromParentOrSelf); + + expect(d).toBeAnInstanceOf(NeedsDirectiveFromParentOrSelf); + expect(d.dependency).toBeAnInstanceOf(SimpleDirective); + }); + it("should get directives from ancestor", () => { var child = parentChildInjectors(ListWrapper.concat([SimpleDirective], extraBindings), [NeedsDirectiveFromAncestor]);