Skip to content

Commit af6a056

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

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

packages/common/src/directives/ng_if.ts

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

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

126126
@Input()
127-
set ngIfElse(templateRef: TemplateRef<NgIfContext>) {
127+
set ngIfElse(templateRef: TemplateRef<NgIfContext>|null) {
128+
assertTemplate('ngIfElse', templateRef);
128129
this._elseTemplateRef = templateRef;
129130
this._elseViewRef = null; // clear previous view if any.
130131
this._updateView();
@@ -163,3 +164,4 @@ export class NgIfContext {
163164
public $implicit: any = null;
164165
public ngIf: any = null;
165166
}
167+

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
@@ -271,8 +271,8 @@ export declare class NgForOfContext<T> {
271271
/** @stable */
272272
export declare class NgIf {
273273
ngIf: any;
274-
ngIfElse: TemplateRef<NgIfContext>;
275-
ngIfThen: TemplateRef<NgIfContext>;
274+
ngIfElse: TemplateRef<NgIfContext> | null;
275+
ngIfThen: TemplateRef<NgIfContext> | null;
276276
constructor(_viewContainer: ViewContainerRef, templateRef: TemplateRef<NgIfContext>);
277277
}
278278

0 commit comments

Comments
 (0)