From af6a0563de479da19442d9b32f3604b6d1429a48 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Sun, 18 Feb 2018 18:41:43 -0800 Subject: [PATCH] fix(common): then and else template might be set to null (#22298) PR Close #22298 --- packages/common/src/directives/ng_if.ts | 6 ++-- packages/common/test/directives/ng_if_spec.ts | 33 ++++++++++++++++++- tools/public_api_guard/common/common.d.ts | 4 +-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/common/src/directives/ng_if.ts b/packages/common/src/directives/ng_if.ts index 07460feb3d..d5953f8d27 100644 --- a/packages/common/src/directives/ng_if.ts +++ b/packages/common/src/directives/ng_if.ts @@ -117,14 +117,15 @@ export class NgIf { } @Input() - set ngIfThen(templateRef: TemplateRef) { + set ngIfThen(templateRef: TemplateRef|null) { this._thenTemplateRef = templateRef; this._thenViewRef = null; // clear previous view if any. this._updateView(); } @Input() - set ngIfElse(templateRef: TemplateRef) { + set ngIfElse(templateRef: TemplateRef|null) { + assertTemplate('ngIfElse', templateRef); this._elseTemplateRef = templateRef; this._elseViewRef = null; // clear previous view if any. this._updateView(); @@ -163,3 +164,4 @@ export class NgIfContext { public $implicit: any = null; public ngIf: any = null; } + diff --git a/packages/common/test/directives/ng_if_spec.ts b/packages/common/test/directives/ng_if_spec.ts index a7ac754601..ef68dd3465 100644 --- a/packages/common/test/directives/ng_if_spec.ts +++ b/packages/common/test/directives/ng_if_spec.ts @@ -138,7 +138,7 @@ import {expect} from '@angular/platform-browser/testing/src/matchers'; expect(fixture.nativeElement).toHaveText('hello'); })); - describe('else', () => { + describe('then/else templates', () => { it('should support else', async(() => { const template = 'TRUE' + 'FALSE'; @@ -169,6 +169,37 @@ import {expect} from '@angular/platform-browser/testing/src/matchers'; expect(fixture.nativeElement).toHaveText('ELSE'); })); + it('should support removing the then/else templates', () => { + const template = ` + Template`; + + fixture = createTestComponent(template); + const comp = fixture.componentInstance; + // then template + comp.booleanCondition = true; + + comp.nestedBooleanCondition = true; + fixture.detectChanges(); + expect(fixture.nativeElement).toHaveText('Template'); + + comp.nestedBooleanCondition = false; + fixture.detectChanges(); + expect(fixture.nativeElement).toHaveText(''); + + // else template + comp.booleanCondition = true; + + comp.nestedBooleanCondition = true; + fixture.detectChanges(); + expect(fixture.nativeElement).toHaveText('Template'); + + comp.nestedBooleanCondition = false; + fixture.detectChanges(); + expect(fixture.nativeElement).toHaveText(''); + }); + it('should support dynamic else', async(() => { const template = 'TRUE' + diff --git a/tools/public_api_guard/common/common.d.ts b/tools/public_api_guard/common/common.d.ts index 172b1163ba..403bf8f8f0 100644 --- a/tools/public_api_guard/common/common.d.ts +++ b/tools/public_api_guard/common/common.d.ts @@ -271,8 +271,8 @@ export declare class NgForOfContext { /** @stable */ export declare class NgIf { ngIf: any; - ngIfElse: TemplateRef; - ngIfThen: TemplateRef; + ngIfElse: TemplateRef | null; + ngIfThen: TemplateRef | null; constructor(_viewContainer: ViewContainerRef, templateRef: TemplateRef); }