<htmlTag *bs-row="'cssClasse1 cssClasse2'">
Created
February 9, 2021 14:21
-
-
Save killan/f88e7723833bd4bb9f8798c178f5cc0b to your computer and use it in GitHub Desktop.
Angular 11 encapsulation system
This file contains 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 { | |
Component, | |
ComponentFactoryResolver, | |
ComponentRef, | |
Directive, ElementRef, | |
Input, OnInit, Renderer2, | |
TemplateRef, | |
ViewContainerRef | |
} from "@angular/core"; | |
@Component({ | |
selector: 'bs-row-container', | |
template: `<ng-container *ngTemplateOutlet="tpl"></ng-container>` | |
}) | |
export class BsRowComponent implements OnInit{ | |
@Input() tpl: TemplateRef<any>; | |
@Input() classes: string; | |
constructor(private el: ElementRef, private renderer: Renderer2) { } | |
ngOnInit() { | |
this.renderer.setAttribute(this.el.nativeElement,'class', "row " + this.classes); | |
} | |
} | |
@Directive({ | |
selector: '[bs-row]' | |
}) | |
export class BsRowDirective implements OnInit { | |
private wrapper: ComponentRef<BsRowComponent>; | |
@Input('bs-row') classes: string; | |
constructor( | |
private tplRef: TemplateRef<any>, | |
private vcRef: ViewContainerRef, | |
private cfr: ComponentFactoryResolver | |
) { | |
} | |
ngOnInit() { | |
const containerFactory = this.cfr.resolveComponentFactory(BsRowComponent); | |
this.wrapper = this.vcRef.createComponent(containerFactory, ); | |
this.wrapper.instance.tpl = this.tplRef; | |
this.wrapper.instance.classes = this.classes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment