From ce5ba807923fdcc5d339f706caeb2967e2a62ab4 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Thu, 4 Aug 2016 20:28:36 +0200 Subject: [PATCH] refactor(NgTemplateOutlet): simplify implementation (#10492) --- .../src/directives/ng_template_outlet.ts | 27 +++++-------------- tools/public_api_guard/common/index.d.ts | 3 ++- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/modules/@angular/common/src/directives/ng_template_outlet.ts b/modules/@angular/common/src/directives/ng_template_outlet.ts index 9ea61e661d..7a32e04da6 100644 --- a/modules/@angular/common/src/directives/ng_template_outlet.ts +++ b/modules/@angular/common/src/directives/ng_template_outlet.ts @@ -6,8 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Directive, EmbeddedViewRef, Input, TemplateRef, ViewContainerRef} from '@angular/core'; -import {isPresent} from '../facade/lang'; +import {Directive, EmbeddedViewRef, Input, OnChanges, TemplateRef, ViewContainerRef} from '@angular/core'; /** * Creates and inserts an embedded view based on a prepared `TemplateRef`. @@ -28,7 +27,7 @@ import {isPresent} from '../facade/lang'; * @experimental */ @Directive({selector: '[ngTemplateOutlet]'}) -export class NgTemplateOutlet { +export class NgTemplateOutlet implements OnChanges { private _viewRef: EmbeddedViewRef; private _context: Object; private _templateRef: TemplateRef; @@ -36,29 +35,17 @@ export class NgTemplateOutlet { constructor(private _viewContainerRef: ViewContainerRef) {} @Input() - set ngOutletContext(context: Object) { - if (this._context !== context) { - this._context = context; - if (isPresent(this._viewRef)) { - this.createView(); - } - } - } + set ngOutletContext(context: Object) { this._context = context; } @Input() - set ngTemplateOutlet(templateRef: TemplateRef) { - if (this._templateRef !== templateRef) { - this._templateRef = templateRef; - this.createView(); - } - } + set ngTemplateOutlet(templateRef: TemplateRef) { this._templateRef = templateRef; } - private createView() { - if (isPresent(this._viewRef)) { + ngOnChanges() { + if (this._viewRef) { this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._viewRef)); } - if (isPresent(this._templateRef)) { + if (this._templateRef) { this._viewRef = this._viewContainerRef.createEmbeddedView(this._templateRef, this._context); } } diff --git a/tools/public_api_guard/common/index.d.ts b/tools/public_api_guard/common/index.d.ts index a30c5e8e80..dcb4616702 100644 --- a/tools/public_api_guard/common/index.d.ts +++ b/tools/public_api_guard/common/index.d.ts @@ -489,10 +489,11 @@ export declare class NgSwitchDefault { } /** @experimental */ -export declare class NgTemplateOutlet { +export declare class NgTemplateOutlet implements OnChanges { ngOutletContext: Object; ngTemplateOutlet: TemplateRef; constructor(_viewContainerRef: ViewContainerRef); + ngOnChanges(): void; } /** @stable */