diff --git a/src/modal/modal-ref.ts b/src/modal/modal-ref.ts index ced43a0772..094a05b6c7 100644 --- a/src/modal/modal-ref.ts +++ b/src/modal/modal-ref.ts @@ -37,10 +37,10 @@ export class NgbModalRef { /** * The instance of a component used for the modal content. * - * When a `TemplateRef` is used as the content, will return `undefined`. + * When a `TemplateRef` is used as the content or when the modal is closed, will return `undefined`. */ get componentInstance(): any { - if (this._contentRef.componentRef) { + if (this._contentRef && this._contentRef.componentRef) { return this._contentRef.componentRef.instance; } } diff --git a/src/modal/modal.spec.ts b/src/modal/modal.spec.ts index 7c9f0c4388..6db15a5722 100644 --- a/src/modal/modal.spec.ts +++ b/src/modal/modal.spec.ts @@ -175,6 +175,7 @@ describe('ngb-modal', () => { modalInstance.close(); fixture.detectChanges(); expect(fixture.nativeElement).not.toHaveModal(); + expect(modalInstance.componentInstance).toBe(undefined); }); it('should open and close modal from inside', () => {