简体   繁体   中英

Calling function in Ionic controller after data loaded

I am pulling data from firebase and depending on the data, I adjust show a different image. My data is taking time to return and the conditional I wrote doesn't do anything because it runs before the data returns. How do I call the function after my data loads? I tried everything on the ionicView Docs . I also tried window.onload but that doesn't work. Thanks for your help in advance.

var firebaseRef = new Firebase("https://app.firebaseio.com/files/0/" + $stateParams.theId);

firebaseRef.once('value', function(dataSnapshot){

var dumData = dataSnapshot.val().data;
//this is just an integer 

    if (dumData > 3){
        document.getElementById("pic").style.backgroundImage = 'url(img/pic2.png';
    }

  //Please ignore syntax errors as they do not exist in original code

Your issue is not related with ionic.

once returns a promise. So you should use the success callback to handle your data.

From Firebase once documentation :

// Provide a failureCallback to be notified when this
// callback is revoked due to security violations. 
firebaseRef.once('value',
  function (dataSnapshot) { 
  // code to handle new value
}, function (err) {
  // code to handle read error
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM