From: Kai Moritz Date: Sat, 21 Dec 2019 11:35:41 +0000 (+0100) Subject: CartService und Buy-Button auf Detail-Seite hinzugefügt X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=02a4f5e131cbac6f4fb820fb8ddd9332892be703;p=examples%2Fangular-tutorial CartService und Buy-Button auf Detail-Seite hinzugefügt --- 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); + } }