Storing and accessing snapshot value from $ rootScope

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!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Storing and accessing snapshot value from $ rootScope

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录