Using $firebase().$on('value', ...) does NOT work
See original GitHub issueI have 2 types of entities stored in Firebase, Cars and Users. Each car has a userID, which is the identity of the user who owns the car. I have a web page where I display data about the car, and I want to display the name of the car’s owner. This means that I need to sync both the car and the user name to the $scope
, so I can display both sets of data on the same web page.
I was previously getting the car and user data using Firebase refs and once('value', ...)
calls, which works just fine:
// get a Firebase reference to the car
var carRef = ref.child('cars').child($scope.carID);
// ...and bind the car to the $scope, so I can display it's data (not shown)
// now use the car's userID to look up the user's name, and bind it to the $scope
carRef.child('userID').once('value', function(userID) {
// use the userID to look up the user, and bind her data to the $scope
// (again not shown, but works just fine)
});
Now I’m trying to do the same thing using the new $firebase() API, which I’ve read about in the new API docs, angularFire-seed project, StackOverflow questions and even online tutorials. But it’s not working for me like it should:
var carRef = ref.child('cars').child($scope.carID);
// get a $firebase reference to the car
var $car = $firebase(carRef);
// now get the $car's userID, so we can look up the user
$car.$child('userID').$on('value', function(userID) {
// this is never called!
});
Based on the firebase API documentation, calling $on('value', ...)
should work just as well as calling on('value', ...)
or once('value', ...)
(as I did above). But the 'value'
event’s callback is never called! If I change 'value'
to 'loaded'
then it does in fact work, but only the first time. If I view another car’s page (a different angular route) and then come back, it doesn’t work. Only if I do a full page refresh does the 'loaded'
event’s callback get called again. Which actually makes complete sense, given the docs for loaded
.
Anyway, why isn’t this working as expected?
Here are the versions I’m using:
- firebase.js 1.0.5
- firebase-simple-login.js 1.2.5
- angular.js 1.2.14
- angular-route.js 1.2.14
- angularfire.js 0.7.0
Issue Analytics
- State:
- Created 10 years ago
- Comments:10 (6 by maintainers)
Top GitHub Comments
Junk, that was incorrect. I think I took one too many bugs to the head this morning. (The $bind() method resolves with an unbind method, not $on).
So the correct usage would be as you stated it:
@romelgomez your comments have been deleted. This thread is over a year old, your comments were unrelated to the original question, and your comments were unrelated to AngularFire. If you want to discuss, reach out on firebase-talk or submit a new issue.