Skip to content

Commit

Permalink
feat(modal): add ability to pass data to modal component (#2293)
Browse files Browse the repository at this point in the history
closes #2290
closes #2275
closes #2251
fixes #2294
  • Loading branch information
IlyaSurmay authored and valorkin committed Jul 28, 2017
1 parent 8ce315b commit 8ac13f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class';
templateUrl: './service-component.html'
})
export class DemoModalServiceFromComponent {
bsModalRef: BsModalRef;
constructor(private modalService: BsModalService) {}

public openModalWithComponent() {
this.modalService.show(ModalContentComponent);
}
let list = ['Open a modal with component', 'Pass your data', 'Do something else', '...'];
this.bsModalRef = this.modalService.show(ModalContentComponent);
this.bsModalRef.content.title = 'Modal with component';
this.bsModalRef.content.list = list;
setTimeout(() => {
list.push('PROFIT!!!');
}, 2000);
}
}

/* This is a component which we pass in modal*/
Expand All @@ -26,15 +33,17 @@ export class DemoModalServiceFromComponent {
</button>
</div>
<div class="modal-body">
This is a modal with component inside.
Click <b>&times;</b> to close modal.
<ul *ngIf="list.length">
<li *ngFor="let item of list">{{item}}</li>
</ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" (click)="bsModalRef.hide()">Close</button>
</div>
`
})
export class ModalContentComponent {
public title: string = 'Modal with component';
public title: string;
public list: any[] = [];
constructor(public bsModalRef: BsModalRef) {}
}
1 change: 1 addition & 0 deletions src/component-loader/component-loader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export class ComponentLoader<T> {
const contentCmptFactory = this._componentFactoryResolver.resolveComponentFactory(content);
const modalContentInjector = ReflectiveInjector.resolveAndCreate([...this._providers, content], this._injector);
const componentRef = contentCmptFactory.create(modalContentInjector);
this._applicationRef.attachView(componentRef.hostView);
return new ContentRef([[componentRef.location.nativeElement]], componentRef.hostView, componentRef);
}
return new ContentRef([[this._renderer.createText(null, `${content}`)]]);
Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal-options.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class BsModalRef {
/**
* Reference to a component inside the modal. Null if modal's been created with TemplateRef
*/
content?: Object | null;
content?: any | null;
}

export const modalConfigDefaults:ModalOptions = {
Expand Down

0 comments on commit 8ac13f9

Please sign in to comment.