refactor(NgTemplateOutlet): simplify implementation (#10492)
This commit is contained in:
parent
8b18ef4ba2
commit
ce5ba80792
|
@ -6,8 +6,7 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Directive, EmbeddedViewRef, Input, TemplateRef, ViewContainerRef} from '@angular/core';
|
import {Directive, EmbeddedViewRef, Input, OnChanges, TemplateRef, ViewContainerRef} from '@angular/core';
|
||||||
import {isPresent} from '../facade/lang';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates and inserts an embedded view based on a prepared `TemplateRef`.
|
* Creates and inserts an embedded view based on a prepared `TemplateRef`.
|
||||||
|
@ -28,7 +27,7 @@ import {isPresent} from '../facade/lang';
|
||||||
* @experimental
|
* @experimental
|
||||||
*/
|
*/
|
||||||
@Directive({selector: '[ngTemplateOutlet]'})
|
@Directive({selector: '[ngTemplateOutlet]'})
|
||||||
export class NgTemplateOutlet {
|
export class NgTemplateOutlet implements OnChanges {
|
||||||
private _viewRef: EmbeddedViewRef<any>;
|
private _viewRef: EmbeddedViewRef<any>;
|
||||||
private _context: Object;
|
private _context: Object;
|
||||||
private _templateRef: TemplateRef<any>;
|
private _templateRef: TemplateRef<any>;
|
||||||
|
@ -36,29 +35,17 @@ export class NgTemplateOutlet {
|
||||||
constructor(private _viewContainerRef: ViewContainerRef) {}
|
constructor(private _viewContainerRef: ViewContainerRef) {}
|
||||||
|
|
||||||
@Input()
|
@Input()
|
||||||
set ngOutletContext(context: Object) {
|
set ngOutletContext(context: Object) { this._context = context; }
|
||||||
if (this._context !== context) {
|
|
||||||
this._context = context;
|
|
||||||
if (isPresent(this._viewRef)) {
|
|
||||||
this.createView();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Input()
|
@Input()
|
||||||
set ngTemplateOutlet(templateRef: TemplateRef<Object>) {
|
set ngTemplateOutlet(templateRef: TemplateRef<Object>) { this._templateRef = templateRef; }
|
||||||
if (this._templateRef !== templateRef) {
|
|
||||||
this._templateRef = templateRef;
|
|
||||||
this.createView();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private createView() {
|
ngOnChanges() {
|
||||||
if (isPresent(this._viewRef)) {
|
if (this._viewRef) {
|
||||||
this._viewContainerRef.remove(this._viewContainerRef.indexOf(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);
|
this._viewRef = this._viewContainerRef.createEmbeddedView(this._templateRef, this._context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -489,10 +489,11 @@ export declare class NgSwitchDefault {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @experimental */
|
/** @experimental */
|
||||||
export declare class NgTemplateOutlet {
|
export declare class NgTemplateOutlet implements OnChanges {
|
||||||
ngOutletContext: Object;
|
ngOutletContext: Object;
|
||||||
ngTemplateOutlet: TemplateRef<Object>;
|
ngTemplateOutlet: TemplateRef<Object>;
|
||||||
constructor(_viewContainerRef: ViewContainerRef);
|
constructor(_viewContainerRef: ViewContainerRef);
|
||||||
|
ngOnChanges(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @stable */
|
/** @stable */
|
||||||
|
|
Loading…
Reference in New Issue