Created
October 2, 2017 17:49
-
-
Save AustinMatherne/659f0aed22efa094d8a55f31aebfe0d4 to your computer and use it in GitHub Desktop.
Angular Let Directive
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, 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Got a
Type 'null' is not assignable to type 'T'
TS error. Works great after making the following change.Thanks for sharing!