Skip to content

Instantly share code, notes, and snippets.

@AustinMatherne
Created October 2, 2017 17:49
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 AustinMatherne/659f0aed22efa094d8a55f31aebfe0d4 to your computer and use it in GitHub Desktop.
Save AustinMatherne/659f0aed22efa094d8a55f31aebfe0d4 to your computer and use it in GitHub Desktop.
Angular Let Directive
import {Directive, Input, TemplateRef, ViewContainerRef} from '@angular/core';
interface LetContext<T> {
ngLet: T;
}
@Directive({
selector: '[ngLet]'
})
export class LetDirective<T> {
private _context: LetContext<T> = {ngLet: null};
constructor(_viewContainer: ViewContainerRef, _templateRef: TemplateRef<LetContext<T>>) {
_viewContainer.createEmbeddedView(_templateRef, this._context);
}
@Input()
set ngLet(value: T) {
this._context.ngLet = value;
}
}
@keego
Copy link

keego commented Nov 15, 2018

Got a Type 'null' is not assignable to type 'T' TS error. Works great after making the following change.

 interface LetContext<T> {
-    ngLet: T;
+    ngLet: T | null;
 }

Thanks for sharing!

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