From 02a4f5e131cbac6f4fb820fb8ddd9332892be703 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 21 Dec 2019 12:35:41 +0100 Subject: [PATCH] =?utf8?q?CartService=20und=20Buy-Button=20auf=20Detail-Se?= =?utf8?q?ite=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/app/cart.service.spec.ts | 12 ++++++++++ src/app/cart.service.ts | 24 +++++++++++++++++++ .../product-details.component.html | 1 + .../product-details.component.ts | 8 ++++++- 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/app/cart.service.spec.ts create mode 100644 src/app/cart.service.ts diff --git a/src/app/cart.service.spec.ts b/src/app/cart.service.spec.ts new file mode 100644 index 0000000..a686b44 --- /dev/null +++ b/src/app/cart.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { CartService } from './cart.service'; + +describe('CartService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: CartService = TestBed.get(CartService); + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/cart.service.ts b/src/app/cart.service.ts new file mode 100644 index 0000000..aeb5d9e --- /dev/null +++ b/src/app/cart.service.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class CartService { + + items= []; + + constructor() { } + + addToCart(product) { + this.items.push(product); + } + + getItems() { + return this.items; + } + + clearCart() { + this.items = []; + return this.items; + } +} diff --git a/src/app/product-details/product-details.component.html b/src/app/product-details/product-details.component.html index a86d6b8..726bc78 100644 --- a/src/app/product-details/product-details.component.html +++ b/src/app/product-details/product-details.component.html @@ -4,4 +4,5 @@

{{ product.name }}

{{ product.price | currency }}

{{ product.description }}

+ diff --git a/src/app/product-details/product-details.component.ts b/src/app/product-details/product-details.component.ts index 049b7cc..c6067b1 100644 --- a/src/app/product-details/product-details.component.ts +++ b/src/app/product-details/product-details.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { products } from '../products'; +import { CartService } from '../cart.service'; @Component({ selector: 'app-product-details', @@ -10,7 +11,9 @@ import { products } from '../products'; export class ProductDetailsComponent implements OnInit { product; - constructor(private route: ActivatedRoute) { } + constructor( + private route: ActivatedRoute, + private cartService: CartService) { } ngOnInit() { this.route.paramMap.subscribe(params => { @@ -18,4 +21,7 @@ export class ProductDetailsComponent implements OnInit { }); } + addToCart(product) { + this.cartService.addToCart(product); + } } -- 2.20.1