I am using Angular CLI + Firebase + AngularFire2 is trying to figure out how to query a single object from Firebase using a key.
The main thread should be to show a list of items, and then click on an item to display a detailed view for that particular item.
This should be a fairly common use case, but the AngularFire 2 docs don't seem to provide clear examples of how I could do this.
I am trying to use a service to request items, but I cannot get it to work.
Component
Service
import { Injectable } from '@angular/core'; import { Headers, Http, Response } from "@angular/http"; import { AngularFire, FirebaseObjectObservable, FirebaseListObservable, FirebaseApp } from 'angularfire2'; import 'rxjs/Rx'; @Injectable() export class ItemsService { private items: FirebaseListObservable<any[]>; private item: FirebaseObjectObservable<any>; constructor(private af: AngularFire) {} getItems(num) { this.items = this.af.database .list('/items', { query: { limitToLast: num | 20} } ) .map( (arr) => { return arr.reverse() } ) as FirebaseListObservable<any[]>; return this.items; } getItem(id: string) { this.item = this.af.database.object('/items/'+id); this.item.subscribe(item => { console.log(item); return item; }); } }
source share