Formular wie in Tutorial ergänzt -- Funzt nicht
authorKai Moritz <kai@juplo.de>
Sat, 21 Dec 2019 14:10:05 +0000 (15:10 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 21 Dec 2019 14:10:05 +0000 (15:10 +0100)
src/app/cart/cart.component.html
src/app/cart/cart.component.ts

index 5ccdc17..7e9bf3d 100644 (file)
@@ -8,3 +8,23 @@
   <span>{{ item.name }}</span>
   <span>{{ item.price | currency }}</span>
 </div>
+
+<form [formGroup]="checkoutForm" (ngSubmit)="onSubmit(checkoutForm.value)">
+
+  <div>
+    <label for="name">
+      Name
+    </label>
+    <input id="name" type="text" formControlName="name">
+  </div>
+
+  <div>
+    <label for="address">
+      Address
+    </label>
+    <input id="address" type="text" formControlName="address">
+  </div>
+
+  <button class="button" type="submit">Purchase</button>
+
+</form>
index 500bd05..3d3bb99 100644 (file)
@@ -1,4 +1,5 @@
 import { Component, OnInit } from '@angular/core';
+import { FormBuilder } from '@angular/forms';
 import { CartService } from '../cart.service';
 
 @Component({
@@ -9,11 +10,26 @@ import { CartService } from '../cart.service';
 export class CartComponent implements OnInit {
 
   items;
+  checkoutForm;
 
-  constructor(private cartService: CartService) { }
+  constructor(
+      private cartService: CartService,
+      private formBuilder: FormBuilder) {
+    this.checkoutForm = this.formBuilder.group({
+      name: '',
+      address: ''
+    });
+  }
 
   ngOnInit() {
     this.items = this.cartService.getItems();
   }
 
+  onSubmit(customerData) {
+    // Process checkout data here
+    console.warn('Your order has been submitted', customerData);
+
+    this.items = this.cartService.clearCart();
+    this.checkoutForm.reset();
+  }
 }