1

As shown in the stackblitz, I'm facing an issue with Angular Material Autocomplete control in which the control stays opened on external events like Page Scroll as shown in the attached sample and the issues even occurs in the Angular Material demo site. Issue Screenshot

Steps to reproduce:

  1. Click on Autocomplete control and let it stay expanded.

  2. Try to do page level scroll to the bottom (https://v7.material.angular.io/components/autocomplete/examples), the Autocomplete control doesn't collapse/close.

  3. I've tried to place focusout event as in the example (https://stackblitz.com/edit/angular-jfuvpb?file=app%2Fautocomplete-overview-example.html), even though the Autocomplete control collapses on Page level scroll click (https://stackblitz.com/edit/angular-jfuvpb?file=app%2Fautocomplete-overview-example.ts) but on more issue came up like the selected option doesn't apply to Autocomplete control.

Expected Behavior: On any action outside of the Autocomplete control e.g., Page level scroll should close the Autocomplete control

Actual Behavior: The Autocomplete control stays expanded and is not user friendly behavior.

BVS
  • 421
  • 1
  • 9
  • 17

1 Answers1

1

It's well-know issue in github

The workaround for that is to put cdkScrollable on your wrapper that has scroll and override MAT_AUTOCOMPLETE_SCROLL_STRATEGY provider.

html

<div class="content-container" cdkScrollable>

Make sure you imported import {ScrollingModule} from '@angular/cdk/scrolling';

Note: you don't need to put cdkScrollable if it is a body scroll

app.module.ts

import { Overlay, CloseScrollStrategy } from '@angular/cdk/overlay';

export function scrollFactory(overlay: Overlay): () => CloseScrollStrategy {
    return () => overlay.scrollStrategies.close();
}

@NgModule({
  ...
  providers: [
    { provide: MAT_AUTOCOMPLETE_SCROLL_STRATEGY, useFactory: scrollFactory, deps: [Overlay] }
  ]
})
export class AppModule {}

Forked Stackblitz

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • Looks like the fix doesn't work in IE11. I think CdkScrolling is not supported in IE11, any other way to fix this for IE11? – BVS Aug 03 '20 at 10:08
  • Maybe it's a problem with Angular Material version? – yurzui Aug 03 '20 at 10:32
  • not really sure as i don't see any precautionary measure on using CDK Scrolling in https://v7.material.angular.io/cdk/scrolling/overview for IE11 – BVS Aug 03 '20 at 11:34
  • Another way is to subscribe to scroll event and call `autoCompleteTrigger.closePanel()` https://stackblitz.com/edit/angular-pvmarv-qljufs?file=src%2Fapp%2Fautocomplete-overview-example.html Also, I'd suggest you to subscribe to scroll event outside of Angular zone for better performance – yurzui Aug 03 '20 at 11:38
  • Apologies missed the above comments, will try it now and get back to you soon. – BVS Aug 07 '20 at 07:02
  • It worked but I've encountered a different issue when multiple Autocomplete controls are present in which only the first Autocomplete closes on Scroll even if the Autocomplete #id is different for the rest of the Auto complete controls. The issue is reproducible in stackblitz sample. Could you please check? https://stackblitz.com/edit/angular-pvmarv-xfnjrd?file=src%2Fapp%2Fautocomplete-overview-example.html – BVS Aug 10 '20 at 15:47
  • I added answer to your addition question – yurzui Aug 10 '20 at 17:09