@@ -488,24 +488,32 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
488
488
489
489
// When the zone is stable initially, and when the option list changes...
490
490
return merge ( firstStable , optionChanges )
491
- . pipe (
492
- // create a new stream of panelClosingActions, replacing any previous streams
493
- // that were created, and flatten it so our stream only emits closing events...
494
- switchMap ( ( ) => {
495
- this . _resetActiveItem ( ) ;
496
- this . autocomplete . _setVisibility ( ) ;
497
-
498
- if ( this . panelOpen ) {
499
- this . _overlayRef ! . updatePosition ( ) ;
500
- }
501
-
502
- return this . panelClosingActions ;
503
- } ) ,
504
- // when the first closing event occurs...
505
- take ( 1 )
506
- )
507
- // set the value, close the panel, and complete.
508
- . subscribe ( event => this . _setValueAndClose ( event ) ) ;
491
+ . pipe (
492
+ // create a new stream of panelClosingActions, replacing any previous streams
493
+ // that were created, and flatten it so our stream only emits closing events...
494
+ switchMap ( ( ) => {
495
+ const wasOpen = this . panelOpen ;
496
+ this . _resetActiveItem ( ) ;
497
+ this . autocomplete . _setVisibility ( ) ;
498
+
499
+ if ( this . panelOpen ) {
500
+ this . _overlayRef ! . updatePosition ( ) ;
501
+
502
+ // If the `panelOpen` state changed, we need to make sure to emit the `opened`
503
+ // event, because we may not have emitted it when the panel was attached. This
504
+ // can happen if the users opens the panel and there are no options, but the
505
+ // options come in slightly later or as a result of the value changing.
506
+ if ( wasOpen !== this . panelOpen ) {
507
+ this . autocomplete . opened . emit ( ) ;
508
+ }
509
+ }
510
+
511
+ return this . panelClosingActions ;
512
+ } ) ,
513
+ // when the first closing event occurs...
514
+ take ( 1 ) )
515
+ // set the value, close the panel, and complete.
516
+ . subscribe ( event => this . _setValueAndClose ( event ) ) ;
509
517
}
510
518
511
519
/** Destroys the autocomplete suggestion panel. */
0 commit comments