Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularFire single object

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.

like image 880
Harry Avatar asked Apr 29 '13 00:04

Harry


1 Answers

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).

like image 137
Anant Avatar answered Oct 18 '22 22:10

Anant