From a5445100050e49e83f73424198d73cd72d672a4d Mon Sep 17 00:00:00 2001 From: Michael Lando Date: Sun, 4 Mar 2018 14:53:33 +0200 Subject: Sync Integ to Master Change-Id: I71e3acc26fa612127756ac04073a522b9cc6cd74 Issue-ID: SDC-977 Signed-off-by: Gitelman, Tal (tg851x) --- .../app/ng2/services/dynamic-component.service.ts | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 catalog-ui/src/app/ng2/services/dynamic-component.service.ts (limited to 'catalog-ui/src/app/ng2/services/dynamic-component.service.ts') diff --git a/catalog-ui/src/app/ng2/services/dynamic-component.service.ts b/catalog-ui/src/app/ng2/services/dynamic-component.service.ts new file mode 100644 index 0000000000..29dd1e9e09 --- /dev/null +++ b/catalog-ui/src/app/ng2/services/dynamic-component.service.ts @@ -0,0 +1,28 @@ +import { + Injectable, Type, ViewContainerRef, ApplicationRef, ComponentFactory, ComponentFactoryResolver, ComponentRef +} from '@angular/core'; + + + +@Injectable() +export class DynamicComponentService { + + constructor(private componentFactoryResolver: ComponentFactoryResolver, private applicationRef: ApplicationRef) { } + + //Creates a component dynamically (aka during runtime). If a view container is not specified, it will append the new component to the app root. + //To subscribe to an event from invoking component: componentRef.instance.clicked.subscribe((m) => console.log(m.name)); + public createDynamicComponent(componentType: Type, viewContainerRef?:ViewContainerRef): ComponentRef { + + viewContainerRef = viewContainerRef || this.getRootViewContainerRef(); + viewContainerRef.clear(); + + let factory: ComponentFactory = this.componentFactoryResolver.resolveComponentFactory(componentType); //Ref: https://angular.io/guide/dynamic-component-loader + let componentRef: ComponentRef = viewContainerRef.createComponent(factory); + return componentRef; + } + + + private getRootViewContainerRef(): ViewContainerRef { + return this.applicationRef.components[0].instance.viewContainerRef; + } +}; \ No newline at end of file -- cgit 1.2.3-korg