import { Injectable } from '@angular/core' ; import { AngularFire , FirebaseListObservable } from 'angularfire2' ; import { Observable } from 'rxjs/Observable' ; import 'rxjs/add/operator/catch' ; import 'rxjs/add/observable/throw' ; export class Category { id : string ; title : string ; desc : string ; imageS : string ; imageL : string ; } @ Injectable ( ) export class CategoryService { private categoriesUrl = 'categories' ; private categories : Category [ ] = [ ] ; constructor ( private af ) { } getCategories ( ) { return this . af . database . list ( this . categoriesUrl ) . catch ( this . handleError ) ; } getCategory ( id ) { for ( let i = 0 ; i < this . categories . length ; i ++ ) { if ( this . categories [ i ] . id === id ) { return this . categories [ i ] ; } } return null ; } private handleError ( error ) { let errMsg = ( error . message ) ? error . message : error . status ? `${ error . status } - ${ error . statusText }` : 'Server error' ; window . alert ( `An error occurred: ${ errMsg }` ) ; return Observable . throw ( errMsg ) ; } }