Skip to content

Commit

Permalink
fix(material/autocomplete): re-enter the Angular zone when the `NgZon…
Browse files Browse the repository at this point in the history
…e.onStable` emits (angular#24569)

The `NgZone.onStable` always emits outside of the Angular zone, but the zone has not been
re-entered. This leads to change detection being called outside of the Angular zone and the
`autocomplete.opened` also was emitting values outside of the Angular zone.
  • Loading branch information
arturovt authored and forsti0506 committed Apr 3, 2022
1 parent d5231ae commit 557fbbc
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,22 +526,27 @@ export abstract class _MatAutocompleteTriggerBase
// create a new stream of panelClosingActions, replacing any previous streams
// that were created, and flatten it so our stream only emits closing events...
switchMap(() => {
const wasOpen = this.panelOpen;
this._resetActiveItem();
this.autocomplete._setVisibility();
this._changeDetectorRef.detectChanges();

if (this.panelOpen) {
this._overlayRef!.updatePosition();

// If the `panelOpen` state changed, we need to make sure to emit the `opened`
// event, because we may not have emitted it when the panel was attached. This
// can happen if the users opens the panel and there are no options, but the
// options come in slightly later or as a result of the value changing.
if (wasOpen !== this.panelOpen) {
this.autocomplete.opened.emit();
// The `NgZone.onStable` always emits outside of the Angular zone, thus we have to re-enter
// the Angular zone. This will lead to change detection being called outside of the Angular
// zone and the `autocomplete.opened` will also emit outside of the Angular.
this._zone.run(() => {
const wasOpen = this.panelOpen;
this._resetActiveItem();
this.autocomplete._setVisibility();
this._changeDetectorRef.detectChanges();

if (this.panelOpen) {
this._overlayRef!.updatePosition();

// If the `panelOpen` state changed, we need to make sure to emit the `opened`
// event, because we may not have emitted it when the panel was attached. This
// can happen if the users opens the panel and there are no options, but the
// options come in slightly later or as a result of the value changing.
if (wasOpen !== this.panelOpen) {
this.autocomplete.opened.emit();
}
}
}
});

return this.panelClosingActions;
}),
Expand Down

0 comments on commit 557fbbc

Please sign in to comment.