Last active
July 25, 2018 08:20
-
-
Save KarolinaCzo/49446ceb833a95a7d3b3f12f9c8fb3f8 to your computer and use it in GitHub Desktop.
How to make a 'window:scroll' Event Listener in Angular6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Use a @HostListener described here: | |
https://stackoverflow.com/questions/41304968/how-to-get-on-scroll-events | |
Examples: | |
https://plnkr.co/edit/jZEmvMVAJ0XoU0eL6dgp?p=info | |
https://plnkr.co/edit/5cs8oMraePEos0xEJbh8?p=preview | |
I used the second one last time I did it. | |
Don't forgt to import @HostListener: | |
import { HostListener } from '@angular/core'; | |
If you see a warning in your code editor: | |
"parameter '$event' implicitly has 'any' type" | |
Here's the solution: | |
https://stackoverflow.com/questions/44521323/angular2-parameter-event-implicitly-has-any-type | |
What helped me is adding 'any' as type of my parameter like: ($event:any). | |
Of course, it's all about the 'window:scroll' event - but this can be used for other events as well. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment