【问题标题】:Angular CDKScrollable not firingAngular CDKScrollable 未触发
【发布时间】:2020-07-29 16:51:15
【问题描述】:

在创建我自己的 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

【问题讨论】:

    标签: javascript angular angular-cdk-virtual-scroll


    【解决方案1】:

    Angular 指令的选择器区分大小写,这意味着您应该使用 cdkScrollable 而不是 CdkScrollable。

    <div class="main-section" id="mainsection" cdkScrollable>
                                              ^^^^^
    

    Forked Stackblitz

    【讨论】:

    • 感谢伙伴..感谢您的帮助!工作。也许你知道为什么,但由于某种原因,当身体有卷轴时我得到一个错误。如果缩小窗口并出现另一个滚动条。如果你使用它会导致出现错误。不过我身上没有 cdkScrollable。
    • window.scroll 事件有一个全局监听器,它不会将数据传递给订阅者
    • 感谢 Yurzui,您将如何附加到 cdkScrollable.. 我有一个修复方法,但如果(数据 === 未定义){ 返回; }
    • 是的,您应该以某种方式对其进行过滤:使用if (!data) returnfilter rxjs 运算符,如这里stackblitz.com/edit/…
    • 感谢 Yurzui 伙伴。无论如何,这个 cdkScrollable 我真的可以听身体吗?
    猜你喜欢
    • 2021-04-10
    • 2019-01-18
    • 1970-01-01
    • 2016-12-06
    • 2018-11-30
    • 2021-06-26
    • 2021-04-24
    • 1970-01-01
    • 2020-01-07
    相关资源
    最近更新 更多