|
| 1 | +import { |
| 2 | + Component, Input, OnInit, |
| 3 | + ComponentFactoryResolver, ViewContainerRef, ViewChild, ElementRef, Renderer2, AfterContentInit |
| 4 | +} from '@angular/core'; |
| 5 | +import { ComponentLoaderService } from '../component-loader.service'; |
| 6 | + |
| 7 | + |
| 8 | +@Component({ |
| 9 | + selector: 'modal-dialog', |
| 10 | + templateUrl: './modal-dialog.component.html', |
| 11 | + styleUrls: ['./modal-dialog.component.css'] |
| 12 | +}) |
| 13 | + |
| 14 | +export class ModalDialogComponent implements OnInit, AfterContentInit { |
| 15 | + @Input() title: string; |
| 16 | + @Input() componentData: string; |
| 17 | + @Input() componentName: any; |
| 18 | + public name : any; |
| 19 | + |
| 20 | + @ViewChild('datacontainer', { read: ViewContainerRef }) entry: ViewContainerRef; |
| 21 | + |
| 22 | + constructor( |
| 23 | + private el: ElementRef, |
| 24 | + private ren: Renderer2, |
| 25 | + private viewContainer: ViewContainerRef, |
| 26 | + private resolver: ComponentFactoryResolver, |
| 27 | + private loaderService :ComponentLoaderService |
| 28 | + ) {} |
| 29 | + |
| 30 | + public div = this.ren.createElement('div'); |
| 31 | + |
| 32 | + ngOnInit() { |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + ngAfterContentInit(){ |
| 37 | + this.ren.addClass(this.el.nativeElement.ownerDocument.body, 'modal-open'); |
| 38 | + this.ren.appendChild(this.el.nativeElement, this.div); |
| 39 | + this.ren.setAttribute(this.div , 'class', 'modal-backdrop fade in'); |
| 40 | + this.createModalPopup(); |
| 41 | + } |
| 42 | + |
| 43 | + createModalPopup(){ |
| 44 | + const name = this.loaderService.getComponent(this.componentName); |
| 45 | + console.log("Component Name => ",name); |
| 46 | + const myFactory = this.resolver.resolveComponentFactory(<any>name); |
| 47 | + const myRef = this.entry.createComponent(myFactory); |
| 48 | + myRef.instance['data'] = this.componentData; |
| 49 | + } |
| 50 | + |
| 51 | + closeModal() { |
| 52 | + this.ren.removeClass(this.el.nativeElement.ownerDocument.body, 'modal-open'); |
| 53 | + this.ren.removeChild(this.el.nativeElement, this.div); |
| 54 | + this.el.nativeElement.remove(); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +} |
| 59 | + |
0 commit comments