Skip to content

Instantly share code, notes, and snippets.

@willgm
Last active January 19, 2017 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willgm/ffe653a49f36997881575df2e93979bc to your computer and use it in GitHub Desktop.
Save willgm/ffe653a49f36997881575df2e93979bc to your computer and use it in GitHub Desktop.
creating template variable contexts
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>) {}
}
@willgm
Copy link
Author

willgm commented Nov 11, 2016

Example of case: avoid multiple observer subscriptions with async pipes everywhere.

Got the inspiration for it here: https://www.youtube.com/watch?v=ZqGvilzDbac&feature=youtu.be&t=46m6s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment