diff --git a/src/app/demo/fake/fake.component.html b/src/app/demo/fake/fake.component.html
index 5696ea7f..b93de07e 100644
--- a/src/app/demo/fake/fake.component.html
+++ b/src/app/demo/fake/fake.component.html
@@ -1,3 +1,4 @@
-
diff --git a/src/app/demo/main/main.component.ts b/src/app/demo/main/main.component.ts
index 8d767416..6c9e876d 100644
--- a/src/app/demo/main/main.component.ts
+++ b/src/app/demo/main/main.component.ts
@@ -93,9 +93,19 @@ export class MainComponent implements AfterViewInit {
this.ngxSmartModalService.create('dynamicModal1', 'Hello, I\'m a simple text !').open();
- this.ngxSmartModalService.create('dynamicModal2', FakeComponent, opts).open();
-
- this.ngxSmartModalService.create('dynamicModal3', this.tpl, opts).open();
+ this.ngxSmartModalService.create('dynamicModal2', this.tpl, opts).open();
+
+ this.ngxSmartModalService.create('dynamicModal3', FakeComponent, opts).open();
+
+ const modal = this.ngxSmartModalService
+ .create('dynamicModal4', FakeComponent, opts)
+ .setData({ lastname: "Frost" })
+ .open();
+
+ setTimeout(() => {
+ modal.setData({ lastname: "Frost (Updated)" }, true);
+ }, 1000);
+
}
}
diff --git a/src/ngx-smart-modal/src/components/ngx-smart-modal.component.ts b/src/ngx-smart-modal/src/components/ngx-smart-modal.component.ts
index 3e0b13d1..3eb6fb92 100644
--- a/src/ngx-smart-modal/src/components/ngx-smart-modal.component.ts
+++ b/src/ngx-smart-modal/src/components/ngx-smart-modal.component.ts
@@ -5,6 +5,7 @@ import {
Component,
ComponentFactory,
ComponentFactoryResolver,
+ ComponentRef,
ElementRef,
EventEmitter,
HostListener,
@@ -17,6 +18,7 @@ import {
QueryList,
Renderer2,
Type,
+ ViewChild,
ViewChildren,
ViewContainerRef
} from '@angular/core';
@@ -100,11 +102,12 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit
public createFrom = 'html';
private _data: any;
+ private _componentRef: ComponentRef
;
@ViewChildren('nsmContent') private nsmContent: QueryList;
@ViewChildren('nsmDialog') public nsmDialog: QueryList;
@ViewChildren('nsmOverlay') private nsmOverlay: QueryList;
- @ViewChildren('dynamicContent', { read: ViewContainerRef }) dynamicContentContainer: QueryList;
+ @ViewChild('dynamicContent', { read: ViewContainerRef }) private dynamicContentContainer: ViewContainerRef;
constructor(
private _renderer: Renderer2,
@@ -127,9 +130,6 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit
if (this.contentComponent) {
const factory = this.componentFactoryResolver.resolveComponentFactory(this.contentComponent);
this.createDynamicContent(this.dynamicContentContainer, factory);
- this.dynamicContentContainer.changes.subscribe((contentViewContainers: QueryList) => {
- this.createDynamicContent(contentViewContainers, factory);
- });
}
}
@@ -244,6 +244,7 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit
public setData(data: any, force?: boolean): NgxSmartModalComponent {
if (!this.hasData() || (this.hasData() && force)) {
this._data = data;
+ this.assignModalDataToComponentData(this._componentRef);
this.onDataAdded.emit(this._data);
this.markForCheck();
}
@@ -255,6 +256,7 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit
* Retrieve the data attached to the modal instance
*/
public getData(): any {
+ this.assignComponentDataToModalData(this._componentRef);
return this._data;
}
@@ -367,11 +369,28 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewInit
/**
* Creates content inside provided ViewContainerRef
*/
- private createDynamicContent(changes: QueryList, factory: ComponentFactory): void {
- changes.forEach((viewContainerRef: ViewContainerRef) => {
- viewContainerRef.clear();
- viewContainerRef.createComponent(factory);
- this.markForCheck();
- });
+ private createDynamicContent(viewContainerRef: ViewContainerRef, factory: ComponentFactory): void {
+ viewContainerRef.clear();
+ this._componentRef = viewContainerRef.createComponent(factory);
+ this.assignModalDataToComponentData(this._componentRef);
+ this.markForCheck();
+ }
+
+ /**
+ * Assigns the modal data to the ComponentRef instance properties
+ */
+ private assignModalDataToComponentData(componentRef: ComponentRef): void {
+ if(componentRef) {
+ Object.assign(componentRef.instance, this._data);
+ }
+ }
+
+ /**
+ * Assigns the ComponentRef instance properties to the modal data object
+ */
+ private assignComponentDataToModalData(componentRef: ComponentRef): void {
+ if(componentRef) {
+ Object.assign(this._data, componentRef.instance);
+ }
}
}