Skip to content

Commit

Permalink
fix(modal): do no crash when accessing componentInstance (#3367)
Browse files Browse the repository at this point in the history
`componentInstance` doesn't crash, but returns undefined when _contentRef is null

Fixes #3366
  • Loading branch information
loicgasser authored and maxokorokov committed Oct 3, 2019
1 parent 40fb0ab commit c60a012
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/modal/modal-ref.ts
Expand Up @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/modal/modal.spec.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit c60a012

Please sign in to comment.