更新时间:2023-11-12 08:32:22
查看 Stackblitz 我创建了。
在您的组件中,通过 ViewChild获取
访问其可滚动面板。然后向面板添加一个事件监听器,当 MatSelect
scrollTop
位置超过a时,会重新加载医生并更新 viewDoctors
数组一定的门槛。
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;
}
}
不要忘记分配 mat-select
到模板中的变量,以便您可以通过 ViewChild
访问它:
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,...