Fireship · @fireship
Infinite Scroll with Angular and Firebase
Uploaded August 8, 2017 · Length 5:15
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.
- 0:00
infinite scroll allows you to query data
- 0:02
on the Fly based on a user scroll
- 0:04
position which is generally a better
- 0:06
user experience than traditional
- 0:08
pagination and it's also easier for you
- 0:10
as a developer to implement in this
- 0:12
episode we're going to build an infinite
- 0:14
scroll feature in angular 4 using
- 0:16
Firebase on the back end to speed up the
- 0:19
development process we're going to use a
- 0:21
package called ngx infinite scroll this
- 0:24
will allow us to listen to as well as
- 0:26
throttle the scroll event which is very
- 0:27
useful when making our queries to
- 0:29
Firebase you can install it by running
- 0:31
npm install ngx infinite scroll then you
- 0:34
can include it in your app module or
- 0:37
whichever module is using it the module
- 0:39
itself exports a directive which we can
- 0:41
use in the HTML we'll get into this more
- 0:43
later but basically it listens for the
- 0:45
scroll event on the window and then
- 0:46
sends a scroll event that we can listen
- 0:49
for at this point let's start building
- 0:51
the service it only has one
- 0:53
responsibility and that's to pull data
- 0:55
from Firebase in this movies collection
- 0:57
which is just a simple collection with
- 1:00
some data about each movie so we can
- 1:02
start by injecting the angular fire
- 1:04
database and then we'll create a
- 1:06
function to get movies the first
- 1:08
argument is the batch size and that's
- 1:10
how many items we want to pull for each
- 1:12
query the last key argument is optional
- 1:15
and that represents the key from the
- 1:16
previous set of records that we pulled
- 1:18
Firebase doesn't have an equivalent to
- 1:19
sqls offset Clause so we need to pass it
- 1:22
a specific key that tells it where to
- 1:24
start the next query so if we get the
- 1:26
last key as an argument then we'll set
- 1:27
it as the start app property before
- 1:29
sending this query to to
- 1:30
Firebase so with that done now we can
- 1:32
start building the component and we'll
- 1:34
actually get started in the HTML first
- 1:37
we're going to Loop over the movies just
- 1:39
like any other observable by passing it
- 1:41
the async pipe then underneath that Loop
- 1:44
we'll create a div that has that
- 1:46
infinite scroll directive that we looked
- 1:47
at earlier the main thing to look at
- 1:50
here is the infinite scroll throttle
- 1:52
which is 1,000 milliseconds or 1 second
- 1:55
then there's the scrolled event which
- 1:57
will emit when the user Scrolls down on
- 1:59
the page the reason we want to throttle


