primeng icon indicating copy to clipboard operation
primeng copied to clipboard

add component template for dialog

Open lilling opened this issue 10 years ago • 4 comments

Instead of writing in the dialog itself the html, I would like to pass a component as a property to the dialog that will hold the html and its functionallity.

It would make it more generic and easy to use.

lilling avatar Aug 04 '16 12:08 lilling

You could do that yourself now. Use a div such as

Then use the ComponentResolver and ViewContainerRef functionality to dynamically load the component just before you show it. I use this technique for editor panels as I don't know what the component will be until the user selects from a list or selects an object from GoogleMaps.

garethlewis avatar Aug 05 '16 08:08 garethlewis

wow thx @garethlewis!

Can you please send a code snippet?

lilling avatar Aug 08 '16 07:08 lilling

This isn't for dialog, but it will do exactly the same thing.

<div id="customPanel" class="right-panel">
    <div class="ui-grid ui-grid-pad ui-fluid">
        <!-- Dynamically load the list of objects here. -->
        <div #objectListContainer></div>        
        <!-- Dynamically load another component here. -->
        <div #childContainer></div>
    </div>
</div>

Add the following import to your component.

import { Directive, Component, ComponentResolver, OnInit, OnDestroy, ViewChild, ViewContainerRef, AfterViewInit, NgZone } from "@angular/core";

Then in your code you will need to declare view children to access the HTML.

@ViewChild("objectListContainer", { read: ViewContainerRef }) objectListContainer: ViewContainerRef;
@ViewChild("childContainer", { read: ViewContainerRef }) childContainer: ViewContainerRef;

Alter your constructor to receive the services for ViewContainer and ComponentResolver. Ignore the socketservice and tokenservice, that is something that I wrote.

constructor(private viewContainer: ViewContainerRef, private componentResolver: ComponentResolver, private router: Router,
        private route: ActivatedRoute, private tokenservice: TokenService, private socketservice: SocketService,
        private domHandler: DomHandler, private ngZone: NgZone)

Then when you decide to show the component, execute the following code.

this.componentResolver.resolveComponent(ObjectListComponent).then(cmpFactory => {
            // this is needed otherwise you will start dropping mulitple copies of the component onto the display
            this.objectListContainer.clear();
            let cmpRef: any = this.objectListContainer.createComponent(cmpFactory);
            cmpRef.instance.objects = this.objectList;
});

Hope that helps. Send me a message if you need more help.

garethlewis avatar Aug 08 '16 07:08 garethlewis

Great example, closing as it is not Dialog related.

cagataycivici avatar May 29 '17 13:05 cagataycivici