swapnilagarwal
swapnilagarwal

Reputation: 1174

Angularfire: How to wait for firebase to load?

In my ionic (angular) app,

  1. How can I wait for firebase to load ?
  2. I want to change app state based on user is authenticated or not.

Currently, firebase.auth().currentUser returns as null.

$ionicPlatform.ready(function(){
    firebase.auth().currentUser --> null
});

But

.controller('MyController', function($scope, $ionicPopup, $firebaseAuth) {
    $scope.$on('$ionicView.afterEnter', function(e) {
        firebase.auth().currentUser --> is valid     
    });
});

Upvotes: 3

Views: 1178

Answers (1)

Sinandro
Sinandro

Reputation: 2646

You can simply use $loaded function which returns a promise which is resolved when the initial server data has been downloaded.

var ref = new Firebase("https://<YOUR_FIREBASE_APP>.firebaseio.com/foo");
var obj = new $firebaseObject(ref);
obj.$loaded().then(function() {
    console.log(obj.$value); // "bar"
});

more info: https://www.firebase.com/docs/web/libraries/angular/guide/synchronized-objects.html

Upvotes: 1

Related Questions