Fireship · @fireship

Infinite Scroll with Angular and Firebase

Uploaded August 8, 2017 · Length 5:15

Infinite Scroll with Angular and Firebase

Summary

build infinite scroll feature in angular 4 using Firebase on the back end

  • angular infinite scroll
  • firebase pagination
  • ngx infinite scroll
  • angular firebase tutorial
  • infinite scrolling

Transcript preview

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

  1. 0:00

    infinite scroll allows you to query data

  2. 0:02

    on the Fly based on a user scroll

  3. 0:04

    position which is generally a better

  4. 0:06

    user experience than traditional

  5. 0:08

    pagination and it's also easier for you

  6. 0:10

    as a developer to implement in this

  7. 0:12

    episode we're going to build an infinite

  8. 0:14

    scroll feature in angular 4 using

  9. 0:16

    Firebase on the back end to speed up the

  10. 0:19

    development process we're going to use a

  11. 0:21

    package called ngx infinite scroll this

  12. 0:24

    will allow us to listen to as well as

  13. 0:26

    throttle the scroll event which is very

  14. 0:27

    useful when making our queries to

  15. 0:29

    Firebase you can install it by running

  16. 0:31

    npm install ngx infinite scroll then you

  17. 0:34

    can include it in your app module or

  18. 0:37

    whichever module is using it the module

  19. 0:39

    itself exports a directive which we can

  20. 0:41

    use in the HTML we'll get into this more

  21. 0:43

    later but basically it listens for the

  22. 0:45

    scroll event on the window and then

  23. 0:46

    sends a scroll event that we can listen

  24. 0:49

    for at this point let's start building

  25. 0:51

    the service it only has one

  26. 0:53

    responsibility and that's to pull data

  27. 0:55

    from Firebase in this movies collection

  28. 0:57

    which is just a simple collection with

  29. 1:00

    some data about each movie so we can

  30. 1:02

    start by injecting the angular fire

  31. 1:04

    database and then we'll create a

  32. 1:06

    function to get movies the first

  33. 1:08

    argument is the batch size and that's

  34. 1:10

    how many items we want to pull for each

  35. 1:12

    query the last key argument is optional

  36. 1:15

    and that represents the key from the

  37. 1:16

    previous set of records that we pulled

  38. 1:18

    Firebase doesn't have an equivalent to

  39. 1:19

    sqls offset Clause so we need to pass it

  40. 1:22

    a specific key that tells it where to

  41. 1:24

    start the next query so if we get the

  42. 1:26

    last key as an argument then we'll set

  43. 1:27

    it as the start app property before

  44. 1:29

    sending this query to to

  45. 1:30

    Firebase so with that done now we can

  46. 1:32

    start building the component and we'll

  47. 1:34

    actually get started in the HTML first

  48. 1:37

    we're going to Loop over the movies just

  49. 1:39

    like any other observable by passing it

  50. 1:41

    the async pipe then underneath that Loop

  51. 1:44

    we'll create a div that has that

  52. 1:46

    infinite scroll directive that we looked

  53. 1:47

    at earlier the main thing to look at

  54. 1:50

    here is the infinite scroll throttle

  55. 1:52

    which is 1,000 milliseconds or 1 second

  56. 1:55

    then there's the scrolled event which

  57. 1:57

    will emit when the user Scrolls down on

  58. 1:59

    the page the reason we want to throttle