Skip to content

Commit

Permalink
fix(modal): use Injector of NgbModal for component content
Browse files Browse the repository at this point in the history
Fixes #982

Closes #983
  • Loading branch information
pkozlowski-opensource committed Nov 2, 2016
1 parent 0e2f386 commit 5172884
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/modal/modal-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export class NgbModalContainer {
ngbModalStack.registerContainer(this);
}

open(moduleCFR: ComponentFactoryResolver, content: string | TemplateRef<any>, options): NgbModalRef {
open(moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: string | TemplateRef<any>, options):
NgbModalRef {
const activeModal = new NgbActiveModal();
const contentRef = this._getContentRef(moduleCFR, content, activeModal);
const contentRef = this._getContentRef(moduleCFR, contentInjector, content, activeModal);
let windowCmptRef: ComponentRef<NgbModalWindow>;
let backdropCmptRef: ComponentRef<NgbModalBackdrop>;
let ngbModalRef: NgbModalRef;
Expand Down Expand Up @@ -64,7 +65,9 @@ export class NgbModalContainer {
});
}

private _getContentRef(moduleCFR: ComponentFactoryResolver, content: any, context: NgbActiveModal): ContentRef {
private _getContentRef(
moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: any,
context: NgbActiveModal): ContentRef {
if (!content) {
return new ContentRef([]);
} else if (content instanceof TemplateRef) {
Expand All @@ -75,7 +78,7 @@ export class NgbModalContainer {
} else {
const contentCmptFactory = moduleCFR.resolveComponentFactory(content);
const modalContentInjector =
ReflectiveInjector.resolveAndCreate([{provide: NgbActiveModal, useValue: context}], this._injector);
ReflectiveInjector.resolveAndCreate([{provide: NgbActiveModal, useValue: context}], contentInjector);
const componentRef = this._viewContainerRef.createComponent(contentCmptFactory, 0, modalContentInjector);
return new ContentRef([[componentRef.location.nativeElement]], componentRef.hostView, componentRef);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal-stack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ describe('modal stack', () => {

it('should throw if a container element was not registered', () => {
const modalStack = new NgbModalStack();
expect(() => { modalStack.open(null, 'foo'); }).toThrowError();
expect(() => { modalStack.open(null, null, 'foo'); }).toThrowError();
});
});
6 changes: 3 additions & 3 deletions src/modal/modal-stack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Injectable, ComponentFactoryResolver} from '@angular/core';
import {Injectable, Injector, ComponentFactoryResolver} from '@angular/core';

import {NgbModalRef} from './modal-ref';
import {NgbModalContainer} from './modal-container';
Expand All @@ -7,13 +7,13 @@ import {NgbModalContainer} from './modal-container';
export class NgbModalStack {
private modalContainer: NgbModalContainer;

open(moduleCFR: ComponentFactoryResolver, content: any, options = {}): NgbModalRef {
open(moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: any, options = {}): NgbModalRef {
if (!this.modalContainer) {
throw new Error(
'Missing modal container, add <template ngbModalContainer></template> to one of your application templates.');
}

return this.modalContainer.open(moduleCFR, content, options);
return this.modalContainer.open(moduleCFR, contentInjector, content, options);
}

registerContainer(modalContainer: NgbModalContainer) { this.modalContainer = modalContainer; }
Expand Down
7 changes: 4 additions & 3 deletions src/modal/modal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Injectable, ComponentFactoryResolver} from '@angular/core';
import {Injectable, Injector, ComponentFactoryResolver} from '@angular/core';

import {NgbModalStack} from './modal-stack';
import {NgbModalRef} from './modal-ref';
Expand Down Expand Up @@ -35,7 +35,8 @@ export interface NgbModalOptions {
*/
@Injectable()
export class NgbModal {
constructor(private _moduleCFR: ComponentFactoryResolver, private _modalStack: NgbModalStack) {}
constructor(
private _moduleCFR: ComponentFactoryResolver, private _injector: Injector, private _modalStack: NgbModalStack) {}

/**
* Opens a new modal window with the specified content and using supplied options. Content can be provided
Expand All @@ -44,6 +45,6 @@ export class NgbModal {
* NgbActiveModal class to close / dismiss modals from "inside" of a component.
*/
open(content: any, options: NgbModalOptions = {}): NgbModalRef {
return this._modalStack.open(this._moduleCFR, content, options);
return this._modalStack.open(this._moduleCFR, this._injector, content, options);
}
}

0 comments on commit 5172884

Please sign in to comment.