Skip to content

Commit d653ffc

Browse files
author
Grzegorz Tokarz
committed
feature/dynamic-content: fix issue with subsequent modal opens
1 parent 03d7691 commit d653ffc

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/ngx-smart-modal/src/components/ngx-smart-modal.component.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
AfterViewInit,
44
ChangeDetectorRef,
55
Component,
6+
ComponentFactory,
67
ComponentFactoryResolver,
78
ElementRef,
89
EventEmitter,
@@ -39,7 +40,7 @@ import { NgxSmartModalConfig } from '../config/ngx-smart-modal.config';
3940
<div class="nsm-body">
4041
<ng-template #dynamicContent></ng-template>
4142
<ng-content></ng-content>
42-
43+
4344
</div>
4445
<button type="button" *ngIf="closable" (click)="close()" aria-label="Close" class="nsm-dialog-btn-close">
4546
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 512 512"
@@ -99,7 +100,7 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit
99100
@ViewChildren('nsmContent') private nsmContent: QueryList<ElementRef>;
100101
@ViewChildren('nsmDialog') public nsmDialog: QueryList<ElementRef>;
101102
@ViewChildren('nsmOverlay') private nsmOverlay: QueryList<ElementRef>;
102-
@ViewChildren('dynamicContent', { read: ViewContainerRef }) dynamicContent: QueryList<ViewContainerRef>;
103+
@ViewChildren('dynamicContent', { read: ViewContainerRef }) dynamicContentContainer: QueryList<ViewContainerRef>;
103104

104105
constructor(
105106
private _renderer: Renderer2,
@@ -118,12 +119,12 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit
118119
}
119120

120121
public ngAfterViewInit(): void {
121-
if (this.dynamicContent && this.contentComponent) {
122+
if (this.contentComponent) {
122123
const factory = this.componentFactoryResolver.resolveComponentFactory(this.contentComponent);
123-
const viewContainerRef: ViewContainerRef = this.dynamicContent.first;
124-
viewContainerRef.clear();
125-
viewContainerRef.createComponent(factory);
126-
this.markForCheck();
124+
this.createDynamicContent(this.dynamicContentContainer, factory);
125+
this.dynamicContentContainer.changes.subscribe((contentViewContainers: QueryList<ViewContainerRef>) => {
126+
this.createDynamicContent(contentViewContainers, factory);
127+
});
127128
}
128129
}
129130

@@ -357,4 +358,15 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit
357358
private get isBrowser(): boolean {
358359
return isPlatformBrowser(this._platformId);
359360
}
361+
362+
/**
363+
* Creates content inside provided ViewContainerRef
364+
*/
365+
private createDynamicContent(changes: QueryList<ViewContainerRef>, factory: ComponentFactory<Component>): void {
366+
changes.forEach((viewContainerRef: ViewContainerRef) => {
367+
viewContainerRef.clear();
368+
viewContainerRef.createComponent(factory);
369+
this.markForCheck();
370+
});
371+
}
360372
}

0 commit comments

Comments
 (0)