Skip to content

Commit

Permalink
fix(modal): missing detectChanges (#4075)
Browse files Browse the repository at this point in the history
Fixes #3960
  • Loading branch information
fbasso committed May 12, 2021
1 parent 99c68ba commit ae2e6b1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/modal/modal-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export class NgbModalStack {

if (backdropCmptRef && backdropCmptRef.instance) {
this._applyBackdropOptions(backdropCmptRef.instance, options);
backdropCmptRef.changeDetectorRef.detectChanges();
}
windowCmptRef.changeDetectorRef.detectChanges();
return ngbModalRef;
}

Expand Down
10 changes: 5 additions & 5 deletions src/modal/modal-window.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DOCUMENT} from '@angular/common';
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Expand Down Expand Up @@ -44,7 +43,7 @@ import {reflow} from '../util/util';
styleUrls: ['./modal.scss']
})
export class NgbModalWindow implements OnInit,
AfterViewInit, OnDestroy {
OnDestroy {
private _closed$ = new Subject<void>();
private _elWithFocus: Element | null = null; // element that is focused prior to modal opening

Expand All @@ -71,9 +70,10 @@ export class NgbModalWindow implements OnInit,

dismiss(reason): void { this.dismissEvent.emit(reason); }

ngOnInit() { this._elWithFocus = this._document.activeElement; }

ngAfterViewInit() { this._show(); }
ngOnInit() {
this._elWithFocus = this._document.activeElement;
this._zone.onStable.asObservable().pipe(take(1)).subscribe(() => { this._show(); });
}

ngOnDestroy() { this._disableEventHandling(); }

Expand Down
4 changes: 4 additions & 0 deletions src/modal/modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,10 @@ describe('ngb-modal', () => {
component.detectChanges();

const modalRef = component.componentInstance.open();

// Ensure that everything works fine after a reflow
document.body.getBoundingClientRect();

let modalEl: HTMLElement | null = null;

modalRef.shown.subscribe(() => {
Expand Down

0 comments on commit ae2e6b1

Please sign in to comment.