Skip to content

Commit 9fd4efe

Browse files
authored
Create modal.directive.ts
1 parent c762d82 commit 9fd4efe

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/app/modal.directive.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Directive, Input, Output, EventEmitter, ElementRef, HostListener, Renderer2, ViewContainerRef, ComponentFactoryResolver } from '@angular/core';
2+
import { ModalDialogComponent } from './modal-dialog/modal-dialog.component';
3+
4+
@Directive({
5+
selector: '[modal-box]'
6+
})
7+
export class ModalDirective {
8+
@Input() title: string;
9+
@Input() componentData: string;
10+
@Input() componentName: string;
11+
12+
13+
@HostListener('click', ['$event'])
14+
15+
/* modal create */
16+
openModal() {
17+
this.createModalDialog(ModalDialogComponent);
18+
}
19+
20+
constructor(
21+
private el: ElementRef,
22+
private ren: Renderer2,
23+
private viewContainer: ViewContainerRef,
24+
private componentFactoryResolver: ComponentFactoryResolver
25+
) { }
26+
27+
ngOnInit() {
28+
// console.log("Title => ", this.title);
29+
// console.log("componentData => ", this.componentData);
30+
// console.log("componentName => ", this.componentName);
31+
}
32+
33+
createModalDialog(modalDialogComponent) {
34+
this.viewContainer.clear();
35+
const modalDialogComponentFactory = this.componentFactoryResolver.resolveComponentFactory(modalDialogComponent);
36+
const modalDialogComponentRef = this.viewContainer.createComponent(modalDialogComponentFactory);
37+
modalDialogComponentRef.instance['title'] = this.title;
38+
modalDialogComponentRef.instance['componentData'] = this.componentData;
39+
modalDialogComponentRef.instance['componentName'] = this.componentName;
40+
41+
return modalDialogComponentRef;
42+
}
43+
44+
45+
46+
}

0 commit comments

Comments
 (0)