Fireship · @fireship

Angular 7 Virtual Scroll - To Infinity and Beyond

Uploaded October 22, 2018 · Length 11:44

Angular 7 Virtual Scroll - To Infinity and Beyond

Summary

Learn how to implement virtual scrolling in Angular 7 with the component development kit (CDK) for high-performance lists and infinite scrolling.

  • angular virtual scroll
  • angular 7 cdk
  • infinite scrolling
  • high-performance lists
  • angular component development kit

Transcript preview

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

  1. 0:00

    [Music]

  2. 0:06

    angular seven is officially here and one

  3. 0:09

    of its coolest new features is the

  4. 0:10

    virtual scroll behavior in the component

  5. 0:12

    development kit it allows you to easily

  6. 0:14

    build a high-performance list that only

  7. 0:16

    renders the items visible to the user in

  8. 0:18

    today's video I'll show you everything

  9. 0:20

    you need to know about virtual scroll

  10. 0:21

    but we'll also take things a few steps

  11. 0:23

    further by making an animated infinite

  12. 0:25

    and real-time if you're new here like

  13. 0:27

    and subscribe and I'm giving away some

  14. 0:29

    winter swag with this video all you have

  15. 0:31

    to do is leave a comment below and we'll

  16. 0:32

    pick a winner on the live stream on

  17. 0:34

    Wednesday so first of all what is

  18. 0:36

    virtual scrolling and why would you want

  19. 0:38

    to use it it's useful when you have a

  20. 0:40

    large list of items to loop over but you

  21. 0:42

    only want to render the items that are

  22. 0:44

    visible in the viewport this should give

  23. 0:45

    you much smoother performance when

  24. 0:47

    working with big lists because the

  25. 0:48

    browser only needs to render a small

  26. 0:50

    subset of the data the cdk provides a

  27. 0:53

    handful of components and directives to

  28. 0:55

    make virtual scrolling super easy in

  29. 0:57

    addition it provides a handful of tools

  30. 0:58

    that makes it much easier to measure the

  31. 1:00

    scrolled element which we can then use

  32. 1:02

    to pull data from our back-end database

  33. 1:03

    in batches giving us an infinite

  34. 1:05

    real-time virtual scroll the first thing

  35. 1:08

    you want to do is make sure you're

  36. 1:09

    up-to-date with angular version 7 then

  37. 1:12

    we can quickly add the cdk to our app by

  38. 1:14

    running ng add angular material you'll

  39. 1:16

    notice that when you do this in angular

  40. 1:18

    7 it gives you a prompt to have

  41. 1:19

    questions to go through you can just

  42. 1:21

    select the defaults and then if you also

  43. 1:23

    want to follow along with the firebase

  44. 1:24

    part you'll want to install angular fire

  45. 1:26

    at this point as well but I'm going to

  46. 1:28

    start with just the cdk basics before we

  47. 1:30

    get to that point if we jump over to my

  48. 1:33

    app module you can see that I'm

  49. 1:34

    importing the scrolling module from the

  50. 1:36

    cdk and then I also have a couple of

  51. 1:38

    components generated just for this demo

  52. 1:40

    so you want to make sure that you have

  53. 1:41

    the scrolling module here in the import

  54. 1:43

    section and then optionally angularfire

  55. 1:45

    for the back-end stuff now that we're

  56. 1:47

    all set up let's start by looking at the

  57. 1:49

    basics of virtual scroll the first thing

  58. 1:51

    you'll need is a big list of something

  59. 1:53

    to loop over so I want to show you how

  60. 1:54

    to generate a ton of fake data really

  61. 1:56

    quickly first I wrote my own helper

  62. 1:58

    function that will return a random emoji