Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(material/autocomplete): re-enter the Angular zone when the NgZone.onStable emits #24569

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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