From 2d0c8f1b0e1aa3386a7c4b374db6f3f0f2f6a603 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Fri, 6 Nov 2015 16:34:41 +0100 Subject: [PATCH] fix(NgFor): allow default templates with ng-for-template Closes #5161 --- .../angular2/src/common/directives/ng_for.ts | 6 +++- .../test/common/directives/ng_for_spec.ts | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/modules/angular2/src/common/directives/ng_for.ts b/modules/angular2/src/common/directives/ng_for.ts index 45656b98f4..b95a1c3e51 100644 --- a/modules/angular2/src/common/directives/ng_for.ts +++ b/modules/angular2/src/common/directives/ng_for.ts @@ -73,7 +73,11 @@ export class NgFor implements DoCheck { } } - set ngForTemplate(value: TemplateRef) { this._templateRef = value; } + set ngForTemplate(value: TemplateRef) { + if (isPresent(value)) { + this._templateRef = value; + } + } doCheck() { if (isPresent(this._differ)) { diff --git a/modules/angular2/test/common/directives/ng_for_spec.ts b/modules/angular2/test/common/directives/ng_for_spec.ts index 62db277763..51be2f62c8 100644 --- a/modules/angular2/test/common/directives/ng_for_spec.ts +++ b/modules/angular2/test/common/directives/ng_for_spec.ts @@ -329,6 +329,39 @@ export function main() { }); })); + it('should use a default template if a custom one is null', + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { + tcb.overrideTemplate(TestComponent, ``) + .overrideTemplate(ComponentUsingTestComponent, '') + .createAsync(ComponentUsingTestComponent) + .then((fixture) => { + var testComponent = fixture.debugElement.componentViewChildren[0]; + testComponent.componentInstance.items = ['a', 'b', 'c']; + fixture.detectChanges(); + expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;'); + + async.done(); + }); + })); + + it('should use a custom template when both default and a custom one are present', + inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => { + tcb.overrideTemplate(TestComponent, ``) + .overrideTemplate( + ComponentUsingTestComponent, + '
  • {{i}}: {{item}};
  • ') + .createAsync(ComponentUsingTestComponent) + .then((fixture) => { + var testComponent = fixture.debugElement.componentViewChildren[0]; + testComponent.componentInstance.items = ['a', 'b', 'c']; + fixture.detectChanges(); + expect(testComponent.nativeElement).toHaveText('0: a;1: b;2: c;'); + + async.done(); + }); + })); }); }