Storing and accessing snapshot value from $ rootScope

综合技术 2018-02-14

In my global controller (assigned to the body in html) I have the following code to grab a snapshot of firebase one time.

ref.once('value', function(snapshot){
    var data = snapshot;
}).then(function(data){
    $rootScope.content = data.val();
});

I am using the content in my HTML templates to render them. All the questions I found had resulted in putting the "then" code inside the callback.

Literally all I need to do is be able to grab a snapshot one time and store it on $rootScope.content
and access it outside of the function, i.e. anywhere in my app.

I'm pretty sure it's because everything is rendering prior to firebase returning anything, I'm just not sure the best way to tell it to wait.

Thank you in advance.

Try:

ref.once('value', function(snapshot){
    $rootScope.content = snapshot.val();
});

Or:

ref.once('value').then(function(snapshot){
    $rootScope.content = snapshot.val();
});

From the
once()

docs:

Return ValueReturns a Promise that can optionally be used instead of the successCallback and failureCallback to handle success and failure.

Hello, buddy!

责编内容by:Hello, buddy! (源链)。感谢您的支持!

您可能感兴趣的

Get Started With Firebase Authentication for iOS Firebas e is a cross-platform real-time mobile database platform that allows coders to fo...
Firebase Test Lab December 2017 Update Doug Stevenson Developer Advocate It's December, folks, and ...
Read and write your Realtime Database with the Fir... Posted by Doug Stevenson, Developer Advocate A long while back, David East wrote ahandy blog post...
Launching Cloud Functions for Firebase v1.0 Source: Launching Cloud Functions for Firebase v1.0 from Firebase Chr...
Using Android Architecture Components with Firebas... Doug Stevenson Developer Advocate Welcome to part 2 of this...