Skip to content
Advertisement

Angular: conditionally render based on whether value was received from Firebase DB

I have a component:

JavaScript

I am currently displaying the data I receive inside a swiper in the HTML template with no problems:

JavaScript

However, I now want to display the part above only after items have been loaded. And display something else (a loading indicator) while it loads. So I am planning something like this:

JavaScript

My only problem is that I don’t know how to update the displayElement variable value when items have been loaded from the firebase db (see first code block). I tried doing a .then(()={}) but valueChanges() does not return a promise.

Many thanks for any help.

Advertisement

Answer

valueChanges() is of type Observable, an Observable is a stream of events or data. To get the data from the database, you need to subscribe to the Observable. Therefore you have to use the subscribe() function:

JavaScript

The subscribe function takes 3 callbacks, the first one will be called whenever the Observable emits an item. Therefore you can change the value of displayElement after getting the first item from the database.

For reference:

http://reactivex.io/documentation/operators/subscribe.html

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement