How to handle window scroll event in Angular 4?

后端 未结 5 1350
暗喜
暗喜 2020-11-29 20:45

I can\'t seem to be able to capture the Window scroll event. On several sites I found code similar to this:

@HostListener(\"window:scroll\", [])
onWindowScro         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 21:01

    If you happen to be using Angular Material, you can do this:

    import { ScrollDispatchModule } from '@angular/cdk/scrolling';
    

    In Ts:

    import { ScrollDispatcher } from '@angular/cdk/scrolling';
    
      constructor(private scrollDispatcher: ScrollDispatcher) {    
        this.scrollDispatcher.scrolled().subscribe(x => console.log('I am scrolling'));
      }
    

    And in Template:

    {{one}}

    Reference: https://material.angular.io/cdk/scrolling/overview

提交回复
热议问题