Created
August 6, 2022 14:50
-
-
Save aniketmuruskar/72f64af6c68a8dc4980fd6b0416f258a to your computer and use it in GitHub Desktop.
Page builder component takes component list as input and adds inside the component host directive
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, Input, OnChanges, OnDestroy, OnInit, SimpleChanges, ViewChild } from '@angular/core'; | |
import { AddComponent } from '../add.component'; | |
import { AppComponentHostDirective } from '../app-component-host.directive'; | |
@Component({ | |
selector: 'app-ui-builder', | |
templateUrl: './app-ui-builder.component.html', | |
styleUrls: [] | |
}) | |
export class AppUIBuilderComponent implements OnInit, OnDestroy, OnChanges { | |
@Input() components!: AddComponent[]; | |
@ViewChild(AppComponentHostDirective, { static: true, read: AppComponentHostDirective }) | |
componentHost!: AppComponentHostDirective; | |
constructor() {} | |
ngOnChanges(changes: SimpleChanges): void { | |
this.buildComponents(); | |
} | |
ngOnInit(): void { | |
this.buildComponents(); | |
} | |
private buildComponents() { | |
this.componentHost.viewContainerRef.clear(); | |
for (const component of this.components) { | |
if (!!component) { | |
const viewContainerRef = this.componentHost.viewContainerRef; | |
const componentRef = viewContainerRef.createComponent<AddComponent>(component.component); | |
componentRef.instance.config = component.config; | |
componentRef.instance.data = component.config.data; | |
} | |
} | |
} | |
ngOnDestroy(): void { | |
this.components = []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment