Last active
January 19, 2017 19:30
-
-
Save willgm/ffe653a49f36997881575df2e93979bc to your computer and use it in GitHub Desktop.
creating template variable contexts
This file contains hidden or 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
| import { Directive, Input, ViewContainerRef, TemplateRef, EmbeddedViewRef } from '@angular/core'; | |
| @Directive({selector: '[context]'}) | |
| export class ContextDirective { | |
| private view: EmbeddedViewRef<any>; | |
| @Input() set contextFrom(context) { | |
| if (!this.view) { | |
| this.view = this.viewContainer.createEmbeddedView(this.template); | |
| } | |
| this.view.context.$implicit = context; | |
| } | |
| constructor(private viewContainer: ViewContainerRef, private template: TemplateRef<any>) {} | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of case: avoid multiple observer subscriptions with
asyncpipes everywhere.Got the inspiration for it here: https://www.youtube.com/watch?v=ZqGvilzDbac&feature=youtu.be&t=46m6s