@@ -45,8 +45,14 @@ app.component.html
4545
4646
4747 ngOnInit() {
48- this.data1="The content is displayed from Demo1 component";
49- this.data2="The content is displayed from Demo2 component";
48+ this.dataObj1 = {
49+ heading: 'Modal Heading One',
50+ content: 'Lorem Ipsum is simply dummy text of the printing and <a href="#">typesetting industry</a>.'
51+ };
52+ this.dataObj2 = {
53+ heading: 'Modal Heading Two',
54+ content: 'Lorem Ipsum is simply dummy text of the printing and <a href="#">typesetting industry</a>.'
55+ };
5056 }
5157
5258 }
@@ -121,14 +127,29 @@ modal.directive.ts
121127 ngOnInit() {
122128 }
123129
130+ openModal() {
131+ console.log('Modal directive is called.');
132+ this.modalElement = this.el.nativeElement;
133+ console.log('modalElement => ', this.modalElement);
134+ this.ren.setAttribute(this.modalElement, 'tabindex', '0');
135+ this.ren.setAttribute(this.modalElement, 'aria-haspopup', 'true');
136+ this.createModalDialog(ModalDialogComponent);
137+ }
138+
124139 createModalDialog(modalDialogComponent) {
140+ console.log('CreateModalDialog is called');
125141 this.viewContainer.clear();
126142 const modalDialogComponentFactory = this.componentFactoryResolver.resolveComponentFactory(modalDialogComponent);
127143 const modalDialogComponentRef = this.viewContainer.createComponent(modalDialogComponentFactory);
128144 modalDialogComponentRef.instance['title'] = this.title;
129145 modalDialogComponentRef.instance['componentData'] = this.componentData;
130146 modalDialogComponentRef.instance['componentName'] = this.componentName;
131-
147+ modalDialogComponentRef.instance['close'].subscribe(event => {
148+ if (event === 'close') {
149+ console.log(this.el.nativeElement);
150+ this.el.nativeElement.focus();
151+ }
152+ });
132153 return modalDialogComponentRef;
133154 }
134155
@@ -197,30 +218,55 @@ modal-dialog.component.ts
197218
198219 public div = this.ren.createElement('div');
199220
200- ngOnInit() {
201-
202- }
203-
204- ngAfterContentInit(){
205- this.ren.addClass(this.el.nativeElement.ownerDocument.body, 'modal-open');
206- this.ren.appendChild(this.el.nativeElement, this.div);
207- this.ren.setAttribute(this.div , 'class', 'modal-backdrop fade in');
208- this.createModalPopup();
209- }
210-
211- createModalPopup(){
212- const name = this.loaderService.getComponent(this.componentName);
213- console.log("Component Name => ",name);
214- const myFactory = this.resolver.resolveComponentFactory(<any>name);
215- const myRef = this.entry.createComponent(myFactory);
216- myRef.instance['data'] = this.componentData;
217- }
218-
219- closeModal() {
220- this.ren.removeClass(this.el.nativeElement.ownerDocument.body, 'modal-open');
221- this.ren.removeChild(this.el.nativeElement, this.div);
222- this.el.nativeElement.remove();
223- }
221+ ngOnInit() {
222+ console.log('Modal Component is called.');
223+ }
224+
225+ ngAfterContentInit() {
226+ this.ren.addClass(this.el.nativeElement.ownerDocument.body, 'modal-open');
227+ this.ren.insertBefore(this.el.nativeElement.children[0], this.div, this.el.nativeElement.children[0].children[0]);
228+ this.ren.setAttribute(this.div , 'class', 'modal-backdrop fade in');
229+ this.ren.setAttribute(this.div , 'tabindex', '-1');
230+ this.createModalPopup();
231+ console.log(this.el.nativeElement.children[0]);
232+ }
233+
234+ ngAfterViewInit() {
235+ this.ren.listen(this.overlayDiv, 'click', (event) => {
236+ this.closeModal();
237+ });
238+ this.ren.listen(this.el.nativeElement, 'keydown', (event) => {
239+ console.log('event keydown => ', event);
240+ if (event.keyCode === 27 || event.key === 'Escape' || event.which === 27) { // ESCAPE key from keyboard
241+ this.closeModal();
242+ event.preventDefault();
243+ }
244+ });
245+ }
246+
247+ createModalPopup() {
248+ console.log('createModalPopup is called.');
249+ const name = this.loaderService.getComponent(this.componentName);
250+ console.log('Component Name => ', name);
251+ const myFactory = this.resolver.resolveComponentFactory(<any>name);
252+ const myRef = this.entry.createComponent(myFactory);
253+ myRef.instance['data'] = this.componentData;
254+ this.overlayDiv = this.el.nativeElement.children[0];
255+ console.log('overlayDiv => ', this.el.nativeElement.children[0]);
256+ this.setFocus();
257+ }
258+
259+ setFocus() {
260+ const focusDiv = this.el.nativeElement.children[0].children[1].children[0];
261+ console.log('focusDiv => ', focusDiv);
262+ focusDiv.focus();
263+ }
264+
265+ closeModal() {
266+ this.ren.removeClass(this.el.nativeElement.ownerDocument.body, 'modal-open');
267+ this.el.nativeElement.remove();
268+ this.close.emit('close');
269+ }
224270
225271
226272 }
0 commit comments