@@ -510,24 +510,32 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnChanges,
510510
511511 // When the zone is stable initially, and when the option list changes...
512512 return merge ( firstStable , optionChanges )
513- . pipe (
514- // create a new stream of panelClosingActions, replacing any previous streams
515- // that were created, and flatten it so our stream only emits closing events...
516- switchMap ( ( ) => {
517- this . _resetActiveItem ( ) ;
518- this . autocomplete . _setVisibility ( ) ;
519-
520- if ( this . panelOpen ) {
521- this . _overlayRef ! . updatePosition ( ) ;
522- }
523-
524- return this . panelClosingActions ;
525- } ) ,
526- // when the first closing event occurs...
527- take ( 1 )
528- )
529- // set the value, close the panel, and complete.
530- . subscribe ( event => this . _setValueAndClose ( event ) ) ;
513+ . pipe (
514+ // create a new stream of panelClosingActions, replacing any previous streams
515+ // that were created, and flatten it so our stream only emits closing events...
516+ switchMap ( ( ) => {
517+ const wasOpen = this . panelOpen ;
518+ this . _resetActiveItem ( ) ;
519+ this . autocomplete . _setVisibility ( ) ;
520+
521+ if ( this . panelOpen ) {
522+ this . _overlayRef ! . updatePosition ( ) ;
523+
524+ // If the `panelOpen` state changed, we need to make sure to emit the `opened`
525+ // event, because we may not have emitted it when the panel was attached. This
526+ // can happen if the users opens the panel and there are no options, but the
527+ // options come in slightly later or as a result of the value changing.
528+ if ( wasOpen !== this . panelOpen ) {
529+ this . autocomplete . opened . emit ( ) ;
530+ }
531+ }
532+
533+ return this . panelClosingActions ;
534+ } ) ,
535+ // when the first closing event occurs...
536+ take ( 1 ) )
537+ // set the value, close the panel, and complete.
538+ . subscribe ( event => this . _setValueAndClose ( event ) ) ;
531539 }
532540
533541 /** Destroys the autocomplete suggestion panel. */
0 commit comments