Fireship · @fireship

Infinite Scroll Pagination with Firestore

Uploaded October 29, 2017 · Length 11:05

Infinite Scroll Pagination with Firestore

Summary

Implement infinite scroll pagination with Firestore and Angular, allowing users to load content in both directions.

  • infinite scroll pagination
  • firestore angular
  • angular pagination
  • infinite scroll angular
  • firestore pagination

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    infinite scroll is a feature that can

  2. 0:01

    have a huge impact on the user

  3. 0:02

    experience by loading data

  4. 0:04

    asynchronously based on scroll position

  5. 0:06

    in this video I'm going to show you how

  6. 0:08

    to implement infinite scroll and angular

  7. 0:11

    with the firestore database but we're

  8. 0:13

    going to do it in a way that not only

  9. 0:14

    allows you to scroll down to load

  10. 0:16

    content but also to scroll up and load

  11. 0:19

    content in the opposite direction this

  12. 0:21

    has been a commonly requested feature

  13. 0:22

    and it would be used if you were doing

  14. 0:24

    something like Facebook Messenger or

  15. 0:26

    slack where you load a timeline of

  16. 0:28

    messages in Reverse we're going to

  17. 0:30

    accomplish this by creating a generic

  18. 0:32

    service in angular and it will not only

  19. 0:34

    retrieve the data but also tell us when

  20. 0:36

    we've reached the end of a collection

  21. 0:37

    and when we are loading new data if

  22. 0:39

    you're new to the channel make sure to

  23. 0:41

    subscribe and grab the source code for

  24. 0:43

    this project from angular firebase comm

  25. 0:45

    I'm going to be starting from a brand

  26. 0:48

    new angular app and it's only dependency

  27. 0:50

    is angular fire so make sure to go to

  28. 0:52

    the documentation and follow the setup

  29. 0:54

    instructions there then the next step is

  30. 0:56

    to create a directive called scrollable

  31. 0:58

    this is going to tell us whether or not

  32. 1:00

    the user is scrolled to either the top

  33. 1:02

    or bottom of the page we're going to do

  34. 1:04

    that by monitoring the scroll event on

  35. 1:06

    the element itself that'd be that

  36. 1:08

    container element and then we're going

  37. 1:09

    to omit our own custom event that will

  38. 1:12

    tell us whether or not it's the bottom

  39. 1:13

    or top you can create a custom event and

  40. 1:15

    angular by using the output decorator

  41. 1:17

    and event emitter so our custom event

  42. 1:20

    here is named scroll position then I'm

  43. 1:23

    going to add element ref to the

  44. 1:25

    constructor but I want to warn you that

  45. 1:26

    using element ref for direct Dom access

  46. 1:29

    can cause issues in angular with

  47. 1:31

    platform server and web worker we can

  48. 1:34

    address this issue by wrapping native

  49. 1:35

    elements in a try-catch block then

  50. 1:37

    provide a fallback such as a load more

  51. 1:39

    button when the Don doesn't actually

  52. 1:41

    exist first we'll set up a host listener

  53. 1:44

    on the element then listen to the scroll

  54. 1:46

    event so keep in mind that your host

  55. 1:48

    element needs to be something scrollable

  56. 1:50

    and we'll set up a function here and put

  57. 1:53

    our try-catch block inside of it

  58. 1:55

    then we need three values from the

  59. 1:57

    native element itself we need scroll top

  60. 1:59

    scroll height and offset height first we