diff --git a/packages/common/src/directives/ng_template_outlet.ts b/packages/common/src/directives/ng_template_outlet.ts
index 489ed87878..69662161e7 100644
--- a/packages/common/src/directives/ng_template_outlet.ts
+++ b/packages/common/src/directives/ng_template_outlet.ts
@@ -55,18 +55,17 @@ export class NgTemplateOutlet implements OnChanges {
const recreateView = this._shouldRecreateView(changes);
if (recreateView) {
+ const viewContainerRef = this._viewContainerRef;
+
if (this._viewRef) {
- this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._viewRef));
+ viewContainerRef.remove(viewContainerRef.indexOf(this._viewRef));
}
- if (this.ngTemplateOutlet) {
- this._viewRef = this._viewContainerRef.createEmbeddedView(
- this.ngTemplateOutlet, this.ngTemplateOutletContext);
- }
- } else {
- if (this._viewRef && this.ngTemplateOutletContext) {
- this._updateExistingContext(this.ngTemplateOutletContext);
- }
+ this._viewRef = this.ngTemplateOutlet ?
+ viewContainerRef.createEmbeddedView(this.ngTemplateOutlet, this.ngTemplateOutletContext) :
+ null;
+ } else if (this._viewRef && this.ngTemplateOutletContext) {
+ this._updateExistingContext(this.ngTemplateOutletContext);
}
}
@@ -96,9 +95,8 @@ export class NgTemplateOutlet implements OnChanges {
}
}
return false;
- } else {
- return true;
}
+ return true;
}
private _updateExistingContext(ctx: Object): void {
diff --git a/packages/common/test/directives/ng_template_outlet_spec.ts b/packages/common/test/directives/ng_template_outlet_spec.ts
index 4f349b1776..57ee45b509 100644
--- a/packages/common/test/directives/ng_template_outlet_spec.ts
+++ b/packages/common/test/directives/ng_template_outlet_spec.ts
@@ -218,6 +218,25 @@ describe('NgTemplateOutlet', () => {
}).not.toThrow();
});
+ it('should not throw when switching from template to null and back to template', async(() => {
+ const template = `foo` +
+ ``;
+ fixture = createTestComponent(template);
+ fixture.detectChanges();
+ const refs = fixture.debugElement.children[0].references !['refs'];
+
+ setTplRef(refs.tplRefs.first);
+ detectChangesAndExpectText('foo');
+
+ setTplRef(null);
+ detectChangesAndExpectText('');
+
+ expect(() => {
+ setTplRef(refs.tplRefs.first);
+ detectChangesAndExpectText('foo');
+ }).not.toThrow();
+ }));
+
});
@Injectable()