Question

Sorry if this is a massively noob question but I'm new to Firebase (though it seems awesome so far).

I have a list of books saved to Firebase from within an Angular app. I can display these on screen fine using the following code:

var db = new Firebase("https://xxxxx.xxxxx.com");
$scope.books = $firebase(db);

Then I loop through the books and spit them out into a list.

When I click on each book I want it to takes me to view that displays the book information. Before Firebase I did this by:

.controller('BookDetailCtrl', function($scope, $stateParams, Books) {
  $scope.book = Books.get($stateParams.bookId);
})

and then spitting the info out on screen.

How can I do the same in Firebase as the books have no id's but have random strings as keys.

I've looked through the docs but have either missed it or I'm looking at things the wrong way...

Any help would be great, thanks

Adding screenshot of firebase forge - I need to access an individual book, how can I get these random IDs firebase uses for keys/id's?

enter image description here

Was it helpful?

Solution

You can utilize $firebase for this:

$scope.book = $scope.books.$child( bookId );

But it's not strictly necessary, as the main purpose of AngularFire is to automate a lot of the list management aspects. You could easily go right to the Firebase API if all we want is a specific book:

var fb = new Firebase('https://kato-sandbox-books.firebaseio-demo.com');
var bookId = 'book1';
$scope.book = null;
fb.child(bookId).on('value', function(snap) {
   $timeout(function() { $scope.book === snap.val(); });
});

See it in action

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