Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created October 12, 2017 07:35
Show Gist options
  • Save tkssharma/685ffe17a9806c9c7be92ff255d02d94 to your computer and use it in GitHub Desktop.
Save tkssharma/685ffe17a9806c9c7be92ff255d02d94 to your computer and use it in GitHub Desktop.
@Injectable()
export class cartService {
constructor(private httpclient : HttpClient) {}
private cartSubject = new Subject<CartState>();
Products : product[]= [];
CartState = this.cartSubject.asObservable();
addProduct(_product:any) {
console.log('in service');
this.Products.push(_product)
this.cartSubject.next(<CartState>{loaded: true, products: this.Products});
}
removeProduct(id:number) {
this.Products = this.Products.filter((_item) => _item.id !== id )
this.cartSubject.next(<CartState>{loaded: false , products: this.Products});
}
getAllProducts() : Observable <any> {
return this.httpclient.get(url).map((res : Response) => res.json())
.catch((error : any) => Observable.throw('Server error'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment