Fireship · @fireship
Infinite Scroll Pagination with Firestore
Uploaded October 29, 2017 · Length 11:05
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.
- 0:00
infinite scroll is a feature that can
- 0:01
have a huge impact on the user
- 0:02
experience by loading data
- 0:04
asynchronously based on scroll position
- 0:06
in this video I'm going to show you how
- 0:08
to implement infinite scroll and angular
- 0:11
with the firestore database but we're
- 0:13
going to do it in a way that not only
- 0:14
allows you to scroll down to load
- 0:16
content but also to scroll up and load
- 0:19
content in the opposite direction this
- 0:21
has been a commonly requested feature
- 0:22
and it would be used if you were doing
- 0:24
something like Facebook Messenger or
- 0:26
slack where you load a timeline of
- 0:28
messages in Reverse we're going to
- 0:30
accomplish this by creating a generic
- 0:32
service in angular and it will not only
- 0:34
retrieve the data but also tell us when
- 0:36
we've reached the end of a collection
- 0:37
and when we are loading new data if
- 0:39
you're new to the channel make sure to
- 0:41
subscribe and grab the source code for
- 0:43
this project from angular firebase comm
- 0:45
I'm going to be starting from a brand
- 0:48
new angular app and it's only dependency
- 0:50
is angular fire so make sure to go to
- 0:52
the documentation and follow the setup
- 0:54
instructions there then the next step is
- 0:56
to create a directive called scrollable
- 0:58
this is going to tell us whether or not
- 1:00
the user is scrolled to either the top
- 1:02
or bottom of the page we're going to do
- 1:04
that by monitoring the scroll event on
- 1:06
the element itself that'd be that
- 1:08
container element and then we're going
- 1:09
to omit our own custom event that will
- 1:12
tell us whether or not it's the bottom
- 1:13
or top you can create a custom event and
- 1:15
angular by using the output decorator
- 1:17
and event emitter so our custom event
- 1:20
here is named scroll position then I'm
- 1:23
going to add element ref to the
- 1:25
constructor but I want to warn you that
- 1:26
using element ref for direct Dom access
- 1:29
can cause issues in angular with
- 1:31
platform server and web worker we can
- 1:34
address this issue by wrapping native
- 1:35
elements in a try-catch block then
- 1:37
provide a fallback such as a load more
- 1:39
button when the Don doesn't actually
- 1:41
exist first we'll set up a host listener
- 1:44
on the element then listen to the scroll
- 1:46
event so keep in mind that your host
- 1:48
element needs to be something scrollable
- 1:50
and we'll set up a function here and put
- 1:53
our try-catch block inside of it
- 1:55
then we need three values from the
- 1:57
native element itself we need scroll top
- 1:59
scroll height and offset height first we


