I want to call a function once I get my firebase data back, however, I don't see any promises function like then on the ref object. Any suggestion? Here is my code:
angular.module('myMod', ['firebase']).controller('myCtrl', ['$scope', '$firebase',
function($scope, $firebase) {
var ref = new Firebase('myfirebaseurl');
$scope.data = $firebase(ref);
// i want to do something like
data.then(function(){
someFunc();
});
// or something like
$scope.$watch('data', function(){
if($scope.data.desiredProperty) {
doSomething();
}
});
}]);
I wasted a sizeable chunk of time trying to do everything with Angularfire when in reality most of the Firebase functionality is more easily implemented without it.
To do what you require, I would suggest the following:
var ref = new Firebase('myfirebaseurl'); // get your ref
// Called once with the initial data and again every time the data changes
ref.on("value", function(data) {
$scope.data = data.val();
someFunc();
});
The code above satisfies your need. It will be called once when the data is retrieved, and again any time the data is updated. If you only want to call it the function the first time the data is loaded you can use:
ref.once("value", function(data) { ... });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With