Skip to content

Commit 0897f49

Browse files
arturovtwagnermaciel
authored andcommitted
fix(material/autocomplete): re-enter the Angular zone when the NgZone.onStable emits (#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. (cherry picked from commit ff01196)
1 parent 1748717 commit 0897f49

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -506,22 +506,27 @@ export abstract class _MatAutocompleteTriggerBase
506506
// create a new stream of panelClosingActions, replacing any previous streams
507507
// that were created, and flatten it so our stream only emits closing events...
508508
switchMap(() => {
509-
const wasOpen = this.panelOpen;
510-
this._resetActiveItem();
511-
this.autocomplete._setVisibility();
512-
this._changeDetectorRef.detectChanges();
513-
514-
if (this.panelOpen) {
515-
this._overlayRef!.updatePosition();
516-
517-
// If the `panelOpen` state changed, we need to make sure to emit the `opened`
518-
// event, because we may not have emitted it when the panel was attached. This
519-
// can happen if the users opens the panel and there are no options, but the
520-
// options come in slightly later or as a result of the value changing.
521-
if (wasOpen !== this.panelOpen) {
522-
this.autocomplete.opened.emit();
509+
// The `NgZone.onStable` always emits outside of the Angular zone, thus we have to re-enter
510+
// the Angular zone. This will lead to change detection being called outside of the Angular
511+
// zone and the `autocomplete.opened` will also emit outside of the Angular.
512+
this._zone.run(() => {
513+
const wasOpen = this.panelOpen;
514+
this._resetActiveItem();
515+
this.autocomplete._setVisibility();
516+
this._changeDetectorRef.detectChanges();
517+
518+
if (this.panelOpen) {
519+
this._overlayRef!.updatePosition();
520+
521+
// If the `panelOpen` state changed, we need to make sure to emit the `opened`
522+
// event, because we may not have emitted it when the panel was attached. This
523+
// can happen if the users opens the panel and there are no options, but the
524+
// options come in slightly later or as a result of the value changing.
525+
if (wasOpen !== this.panelOpen) {
526+
this.autocomplete.opened.emit();
527+
}
523528
}
524-
}
529+
});
525530

526531
return this.panelClosingActions;
527532
}),

0 commit comments

Comments
 (0)