Question

In the angularFire examples it shows how to get a collection of objects from firebase.

app.controller('ctrl', ['$scope', '$timeout', 'angularFireCollection',
  function($scope, $timeout, angularFireCollection) {    
    var url = 'https://ex.firebaseio.com/stuff';
    $scope.col = angularFireCollection(url);
  }
]);

What about a single object only?

I tried something like this:

  fb.child('stuff/'+id).on('value', function(snapshot) {
    $scope.obj = snapshot.val();
    console.log('hey got the value')
    console.log(snapshot.val())
  });

Doesn't seem to work. The console outputs the object value correctly but the controller doesn't update.

Was it helpful?

Solution

Try using the regular angularFire service, and specifying the type of your single object:

app.controller('ctrl', ['$scope', '$timeout', 'angularFire',
  function($scope, $timeout, angularFire) {    
    var url = 'https://ex.firebaseio.com/stuff';
    angularFire(url, $scope, "obj", "");
  }
]);

Note the 4th argument (" " means string, you can also use booleans, numbers, objects and arrays).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top