Check out the Stackblitz I created.
In your component, get the MatSelect
via ViewChild
to access its scrollable panel. Then add an event listener to the panel, which reloads the doctors and updated the viewDoctors
array when the scrollTop
position exceeds a certain threshold.
allDoctors = ['doctor', 'doctor', ..., 'doctor'];
viewDoctors = this.allDoctors.slice(0, 10);
private readonly RELOAD_TOP_SCROLL_POSITION = 100;
@ViewChild('doctorSelect') selectElem: MatSelect;
ngOnInit() {
this.selectElem.onOpen.subscribe(() => this.registerPanelScrollEvent());
}
registerPanelScrollEvent() {
const panel = this.selectElem.panel.nativeElement;
panel.addEventListener('scroll', event => this.loadAllOnScroll(event));
}
loadAllOnScroll(event) {
if (event.target.scrollTop > this.RELOAD_TOP_SCROLL_POSITION) {
this.viewDoctors = this.allDoctors;
}
}
Don't forget to assign your mat-select
to a variable in your template so that you can access it via ViewChild
:
<mat-form-field>
<mat-select placeholder="Choose a Doctor" #doctorSelect>
^^^^^^^^^^^^^
<mat-option *ngFor="let dr of viewDoctors;let i = index">
{{dr}}
</mat-option>
</mat-select>
</mat-form-field>
This is only a very basic setup illustrating the idea. You might want to do show a loading animation, cleanup the event listener,...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…