I am trying to use scroll after opening Material Autocomplete to scroll to a specific option using _setScrollTop. It is working fine when I use setTimeout but without it nothing is happening. Is there a way to avoid using timeout?
Here is a live example.
I am using @ViewChild to get autocomplete:
@ViewChild('auto') testAuto: MatAutocomplete;
And then calling autoOpened() function on Autocomplete (opened)
<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete" (opened)="autoOpened()">
autoOpened() {
// Not working
this.testAuto._setScrollTop(30);
// Working
// setTimeout(() => {
// this.testAuto._setScrollTop(30);
// })
}
I don't think there is a way to avoid setTimeout in this case. Because autocomplete panel doesn't exist at that moment yet.
See Angular Autocomplete sources:
_setScrollTop(scrollTop: number): void {
if (this.panel) {
this.panel.nativeElement.scrollTop = scrollTop;
}
}