From c60a0121e7c9a0bb233d46008d146292409af63f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gasser=20Lo=C3=AFc?= Date: Thu, 3 Oct 2019 04:29:21 -0400 Subject: [PATCH] fix(modal): do no crash when accessing `componentInstance` (#3367) `componentInstance` doesn't crash, but returns undefined when _contentRef is null Fixes #3366 --- src/modal/modal-ref.ts | 4 ++-- src/modal/modal.spec.ts | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) 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', () => {