2016-06-06 19:03:32 +03:00
|
|
|
import {Directive, Input, TemplateRef, ViewContainerRef, EmbeddedViewRef} from '@angular/core';
|
2016-05-31 15:22:59 -07:00
|
|
|
import {isPresent} from '../facade/lang';
|
2016-04-11 17:47:28 +02:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2016-04-11 17:47:28 +02:00
|
|
|
/**
|
|
|
|
* Creates and inserts an embedded view based on a prepared `TemplateRef`.
|
2016-06-06 19:03:32 +03:00
|
|
|
* You can attach a context object to the `EmbeddedViewRef` by setting `[ngOutletContext]`.
|
|
|
|
* `[ngOutletContext]` should be an object, the object's keys will be the local template variables
|
|
|
|
* available within the `TemplateRef`.
|
|
|
|
*
|
|
|
|
* Note: using the key `$implicit` in the context object will set it's value as default.
|
2016-04-11 17:47:28 +02:00
|
|
|
*
|
|
|
|
* ### Syntax
|
2016-06-06 19:03:32 +03:00
|
|
|
* - `<template [ngTemplateOutlet]="templateRefExpression" [ngOutletContext]="objectExpression"></template>`
|
2016-05-27 11:24:05 -07:00
|
|
|
*
|
|
|
|
* @experimental
|
2016-04-11 17:47:28 +02:00
|
|
|
*/
|
|
|
|
@Directive({selector: '[ngTemplateOutlet]'})
|
|
|
|
export class NgTemplateOutlet {
|
2016-06-06 19:03:32 +03:00
|
|
|
private _viewRef: EmbeddedViewRef<any>;
|
|
|
|
private _context: Object;
|
|
|
|
private _templateRef: TemplateRef<any>;
|
2016-04-11 17:47:28 +02:00
|
|
|
|
|
|
|
constructor(private _viewContainerRef: ViewContainerRef) {}
|
|
|
|
|
2016-06-06 19:03:32 +03:00
|
|
|
@Input()
|
|
|
|
set ngOutletContext(context: Object) {
|
|
|
|
if (this._context !== context) {
|
|
|
|
this._context = context;
|
|
|
|
if (isPresent(this._viewRef)) {
|
|
|
|
this.createView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 17:47:28 +02:00
|
|
|
@Input()
|
2016-04-28 14:00:31 -07:00
|
|
|
set ngTemplateOutlet(templateRef: TemplateRef<Object>) {
|
2016-06-06 19:03:32 +03:00
|
|
|
if (this._templateRef !== templateRef) {
|
|
|
|
this._templateRef = templateRef;
|
|
|
|
this.createView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private createView() {
|
|
|
|
if (isPresent(this._viewRef)) {
|
|
|
|
this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._viewRef));
|
2016-04-11 17:47:28 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 19:03:32 +03:00
|
|
|
if (isPresent(this._templateRef)) {
|
|
|
|
this._viewRef = this._viewContainerRef.createEmbeddedView(this._templateRef, this._context);
|
2016-04-11 17:47:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|