Guest User

Untitled

a guest
Jul 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. //declaring a cart model for products to add
  2.  
  3. export class Cart{
  4. id:string;
  5. name:string;
  6. quantity:number;
  7. picture:string;
  8. }
  9.  
  10. import { Injectable, Inject } from "@angular/core";
  11. import { WebStorageService, LOCAL_STORAGE } from "angular-webstorage-service";
  12. import { Cart } from "./models/cart.model";
  13.  
  14. @Injectable({providedIn:'root'})
  15. export class AppService{
  16. key="Product";
  17. mycart:Cart[]=[];//creating a cart array
  18. constructor(@Inject(LOCAL_STORAGE) private storage: WebStorageService) {
  19.  
  20. }
  21.  
  22. addtocart(id:string,name:string,quantity:number,picture:string){
  23. var newcart:Cart={id:id,name:name,quantity:quantity,picture:picture};
  24. this.mycart.push(newcart);//this gives me error
  25. this.storage.set(this.key, this.mycart);//saving cart to the local storage
  26.  
  27. }
  28.  
  29. getproducts(){
  30. this.mycart= this.storage.get(this.key);
  31. console.log(this.mycart);
  32. return this.mycart;
  33. }
  34.  
  35.  
  36.  
  37. }
Add Comment
Please, Sign In to add comment