forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(NgTemplateOutlet): add context to NgTemplateOutlet
Closes angular#9042
- Loading branch information
1 parent
7deae89
commit eab91f7
Showing
3 changed files
with
118 additions
and
8 deletions.
There are no files selected for viewing
39 changes: 31 additions & 8 deletions
39
modules/@angular/common/src/directives/ng_template_outlet.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,53 @@ | ||
import {Directive, Input, TemplateRef, ViewContainerRef, ViewRef} from '@angular/core'; | ||
|
||
import {Directive, Input, TemplateRef, ViewContainerRef, EmbeddedViewRef} from '@angular/core'; | ||
import {isPresent} from '../facade/lang'; | ||
|
||
|
||
/** | ||
* Creates and inserts an embedded view based on a prepared `TemplateRef`. | ||
* 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. | ||
* | ||
* ### Syntax | ||
* - `<template [ngTemplateOutlet]="templateRefExpression"></template>` | ||
* - `<template [ngTemplateOutlet]="templateRefExpression" [ngOutletContext]="objectExpression"></template>` | ||
* | ||
* @experimental | ||
*/ | ||
@Directive({selector: '[ngTemplateOutlet]'}) | ||
export class NgTemplateOutlet { | ||
private _insertedViewRef: ViewRef; | ||
private _viewRef: EmbeddedViewRef<any>; | ||
private _context: Object; | ||
private _templateRef: TemplateRef<any>; | ||
|
||
constructor(private _viewContainerRef: ViewContainerRef) {} | ||
|
||
@Input() | ||
set ngOutletContext(context: Object) { | ||
if (this._context !== context) { | ||
this._context = context; | ||
if (isPresent(this._viewRef)) { | ||
this.createView(); | ||
} | ||
} | ||
} | ||
|
||
@Input() | ||
set ngTemplateOutlet(templateRef: TemplateRef<Object>) { | ||
if (isPresent(this._insertedViewRef)) { | ||
this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._insertedViewRef)); | ||
if (this._templateRef !== templateRef) { | ||
this._templateRef = templateRef; | ||
this.createView(); | ||
} | ||
} | ||
|
||
private createView() { | ||
if (isPresent(this._viewRef)) { | ||
this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._viewRef)); | ||
} | ||
|
||
if (isPresent(templateRef)) { | ||
this._insertedViewRef = this._viewContainerRef.createEmbeddedView(templateRef); | ||
if (isPresent(this._templateRef)) { | ||
this._viewRef = this._viewContainerRef.createEmbeddedView(this._templateRef, this._context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
eab91f7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any working example in plnkr for this?