javascript - Angular CDKScrollable 未触发
问题描述
创建我自己的 div 时,我似乎无法触发有角度的 CdkScrollable:
<div class="main-section" id="mainsection" #mainsection CdkScrollable>
<div class="content" style="height: 300px; background-color: red; margin-bottom: 30px; "></div>
<div class="content" style="height: 300px; background-color: red; margin-bottom: 30px;"></div>
<div class="content" style="height: 300px; background-color: red; margin-bottom: 30px;"></div>
<div class="content" style="height: 300px; background-color: red; margin-bottom: 30px;"></div>
<div class="content" style="height: 300px; background-color: red; margin-bottom: 30px;"></div>
<div class="content" style="height: 300px; background-color: red; margin-bottom: 30px;"></div>
</div>
#mainsection {
height: 400px;
display: block;
overflow-y: auto;
}
private onWindowScroll(data: CdkScrollable) {
const scrollTop = data.getElementRef().nativeElement.scrollTop || 0;
console.log(scrollTop);
if (this.lastOffset > scrollTop) {
console.log('Show toolbar');
} else if (scrollTop < 10) {
console.log('Show toolbar');
} else if (scrollTop > 100) {
console.log('Hide toolbar');
}
this.lastOffset = scrollTop;
}
ngAfterViewInit() {
console.log('hiiii');
this.scrollingSubscription = this.scroll
.scrolled()
.subscribe((data: CdkScrollable) => {
this.onWindowScroll(data);
});
}
https://stackblitz.com/edit/angular-9-material-starter-ykpx6o?file=src%2Fapp%2Fapp.component.ts
解决方案
Angular 指令的选择器区分大小写,这意味着您应该使用c dkScrollable 而不是C dkScrollable。
<div class="main-section" id="mainsection" cdkScrollable>
^^^^^
推荐阅读
- javascript - 如何选择具有“角色”属性的元素?
- c++ - 获取类成员函数中的参数数量
- azure-functions - 如何更新使用 Azure Function 部署的机器学习模型
- python - Jupyter 中的 Cython ImportError
- android - TWA 中的推送通知在 Chrome 图标中显示一个指示器,而不是应用程序图标
- powershell - 有没有办法使用“调用 Web 请求”直接在 Chrome 浏览器应用程序中打开 Uri?
- logstash - 由于错误,Logstash 停止处理:(SystemExit) exit
- python - python数据表,列上的字符串操作
- julia - 使用 StaticArrays 赋值时如何实现类型稳定性?
- r - 如何检索特定日期和代码的股票价格?