import { Injectable } from '@angular/core' ; import { Headers , Http , Response } from '@angular/http' ; import 'rxjs/add/operator/toPromise' ; export class Category { id : string ; title : string ; desc : string ; imageS : string ; imageL : string ; } @ Injectable ( ) export class CategoryService { private categoriesUrl = 'app/categories' ; private categories : Category [ ] = [ ] ; constructor ( private http ) { } getCategories ( ) { return this . http . get ( this . categoriesUrl ) . toPromise ( ) . then ( ( response ) => { this . categories = response . json ( ) . data as Category [ ] ; return this . categories ; } ) . 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 ) { window . alert ( `An error occurred: ${ error }` ) ; return Promise . reject ( error . message || error ) ; } }