Skip to content

Commit 8115edc

Browse files
committed
fix(common): then and else template might be set to null (angular#22298)
PR Close angular#22298
1 parent a8b5465 commit 8115edc

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

packages/common/src/directives/ng_if.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ export class NgIf {
117117
}
118118

119119
@Input()
120-
set ngIfThen(templateRef: TemplateRef<NgIfContext>) {
120+
set ngIfThen(templateRef: TemplateRef<NgIfContext>|null) {
121121
assertTemplate('ngIfThen', templateRef);
122122
this._thenTemplateRef = templateRef;
123123
this._thenViewRef = null; // clear previous view if any.
124124
this._updateView();
125125
}
126126

127127
@Input()
128-
set ngIfElse(templateRef: TemplateRef<NgIfContext>) {
128+
set ngIfElse(templateRef: TemplateRef<NgIfContext>|null) {
129129
assertTemplate('ngIfElse', templateRef);
130130
this._elseTemplateRef = templateRef;
131131
this._elseViewRef = null; // clear previous view if any.
@@ -166,9 +166,9 @@ export class NgIfContext {
166166
public ngIf: any = null;
167167
}
168168

169-
function assertTemplate(property: string, templateRef: TemplateRef<any>): void {
170-
const isTemplateRef = templateRef.createEmbeddedView != null;
171-
if (!isTemplateRef) {
169+
function assertTemplate(property: string, templateRef: TemplateRef<any>| null): void {
170+
const isTemplateRefOrNull = !!(!templateRef || templateRef.createEmbeddedView);
171+
if (!isTemplateRefOrNull) {
172172
throw new Error(`${property} must be a TemplateRef, but received '${stringify(templateRef)}'.`);
173173
}
174174
}

packages/common/test/directives/ng_if_spec.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
138138
expect(fixture.nativeElement).toHaveText('hello');
139139
}));
140140

141-
describe('else', () => {
141+
describe('then/else templates', () => {
142142
it('should support else', async(() => {
143143
const template = '<span *ngIf="booleanCondition; else elseBlock">TRUE</span>' +
144144
'<ng-template #elseBlock>FALSE</ng-template>';
@@ -169,6 +169,37 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
169169
expect(fixture.nativeElement).toHaveText('ELSE');
170170
}));
171171

172+
it('should support removing the then/else templates', () => {
173+
const template = `<span *ngIf="booleanCondition;
174+
then nestedBooleanCondition ? tplRef : null;
175+
else nestedBooleanCondition ? tplRef : null"></span>
176+
<ng-template #tplRef>Template</ng-template>`;
177+
178+
fixture = createTestComponent(template);
179+
const comp = fixture.componentInstance;
180+
// then template
181+
comp.booleanCondition = true;
182+
183+
comp.nestedBooleanCondition = true;
184+
fixture.detectChanges();
185+
expect(fixture.nativeElement).toHaveText('Template');
186+
187+
comp.nestedBooleanCondition = false;
188+
fixture.detectChanges();
189+
expect(fixture.nativeElement).toHaveText('');
190+
191+
// else template
192+
comp.booleanCondition = true;
193+
194+
comp.nestedBooleanCondition = true;
195+
fixture.detectChanges();
196+
expect(fixture.nativeElement).toHaveText('Template');
197+
198+
comp.nestedBooleanCondition = false;
199+
fixture.detectChanges();
200+
expect(fixture.nativeElement).toHaveText('');
201+
});
202+
172203
it('should support dynamic else', async(() => {
173204
const template =
174205
'<span *ngIf="booleanCondition; else nestedBooleanCondition ? b1 : b2">TRUE</span>' +

tools/public_api_guard/common/common.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ export declare class NgForOfContext<T> {
275275
/** @stable */
276276
export declare class NgIf {
277277
ngIf: any;
278-
ngIfElse: TemplateRef<NgIfContext>;
279-
ngIfThen: TemplateRef<NgIfContext>;
278+
ngIfElse: TemplateRef<NgIfContext> | null;
279+
ngIfThen: TemplateRef<NgIfContext> | null;
280280
constructor(_viewContainer: ViewContainerRef, templateRef: TemplateRef<NgIfContext>);
281281
}
282282

0 commit comments

Comments
 (0)