diff --git a/src/app/directives/directives.module.ts b/src/app/directives/directives.module.ts new file mode 100644 index 0000000..e613d00 --- /dev/null +++ b/src/app/directives/directives.module.ts @@ -0,0 +1,17 @@ +import { NgModule } from '@angular/core'; + +import { NgLetDirective } from './ng-let.directive'; + +const exportedDirectives = [ + NgLetDirective +]; + +@NgModule({ + declarations: [ + ...exportedDirectives + ], + exports: [ + ...exportedDirectives + ] +}) +export class DirectivesModule { } \ No newline at end of file diff --git a/src/app/directives/ng-let.directive.ts b/src/app/directives/ng-let.directive.ts new file mode 100644 index 0000000..19e54a6 --- /dev/null +++ b/src/app/directives/ng-let.directive.ts @@ -0,0 +1,34 @@ +import { + Directive, + Input, + OnInit, + TemplateRef, + ViewContainerRef +} from '@angular/core'; + +export class NgLetContext { + $implicit: any = undefined; + ngLet: any = undefined; +} + +@Directive({ + // tslint:disable-next-line:directive-selector + selector: '[ngLet]' +}) +export class NgLetDirective implements OnInit { + private readonly _context = new NgLetContext(); + + @Input() + set ngLet(value: any) { + this._context.$implicit = this._context.ngLet = value; + } + + constructor( + private readonly _vcr: ViewContainerRef, + private readonly _templateRef: TemplateRef + ) { } + + ngOnInit(): void { + this._vcr.createEmbeddedView(this._templateRef, this._context); + } +} \ No newline at end of file