Skip to content

Commit 8ac13f9

Browse files
IlyaSurmayvalorkin
authored andcommitted
feat(modal): add ability to pass data to modal component (#2293)
closes #2290 closes #2275 closes #2251 fixes #2294
1 parent 8ce315b commit 8ac13f9

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

demo/src/app/components/+modal/demos/service-component/service-component.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class';
77
templateUrl: './service-component.html'
88
})
99
export class DemoModalServiceFromComponent {
10+
bsModalRef: BsModalRef;
1011
constructor(private modalService: BsModalService) {}
1112

1213
public openModalWithComponent() {
13-
this.modalService.show(ModalContentComponent);
14-
}
14+
let list = ['Open a modal with component', 'Pass your data', 'Do something else', '...'];
15+
this.bsModalRef = this.modalService.show(ModalContentComponent);
16+
this.bsModalRef.content.title = 'Modal with component';
17+
this.bsModalRef.content.list = list;
18+
setTimeout(() => {
19+
list.push('PROFIT!!!');
20+
}, 2000);
21+
}
1522
}
1623

1724
/* This is a component which we pass in modal*/
@@ -26,15 +33,17 @@ export class DemoModalServiceFromComponent {
2633
</button>
2734
</div>
2835
<div class="modal-body">
29-
This is a modal with component inside.
30-
Click <b>&times;</b> to close modal.
36+
<ul *ngIf="list.length">
37+
<li *ngFor="let item of list">{{item}}</li>
38+
</ul>
3139
</div>
3240
<div class="modal-footer">
3341
<button type="button" class="btn btn-default" (click)="bsModalRef.hide()">Close</button>
3442
</div>
3543
`
3644
})
3745
export class ModalContentComponent {
38-
public title: string = 'Modal with component';
46+
public title: string;
47+
public list: any[] = [];
3948
constructor(public bsModalRef: BsModalRef) {}
4049
}

src/component-loader/component-loader.class.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ export class ComponentLoader<T> {
276276
const contentCmptFactory = this._componentFactoryResolver.resolveComponentFactory(content);
277277
const modalContentInjector = ReflectiveInjector.resolveAndCreate([...this._providers, content], this._injector);
278278
const componentRef = contentCmptFactory.create(modalContentInjector);
279+
this._applicationRef.attachView(componentRef.hostView);
279280
return new ContentRef([[componentRef.location.nativeElement]], componentRef.hostView, componentRef);
280281
}
281282
return new ContentRef([[this._renderer.createText(null, `${content}`)]]);

src/modal/modal-options.class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class BsModalRef {
3939
/**
4040
* Reference to a component inside the modal. Null if modal's been created with TemplateRef
4141
*/
42-
content?: Object | null;
42+
content?: any | null;
4343
}
4444

4545
export const modalConfigDefaults:ModalOptions = {

0 commit comments

Comments
 (0)