iT邦幫忙

0

第48天~

Tzu 2021-08-31 20:26:311119 瀏覽
  • 分享至 

  • xImage
  •  

這個得上一篇:https://ithelp.ithome.com.tw/articles/10258795
從 checkout.component.html檔案

現在要做這個:同住址按V的BOX
https://ithelp.ithome.com.tw/upload/images/20210907/20119035AqQPazZdFO.png

<div class="main-content page-m">
  <div class="section-content section-content-p30">
      <div class="container-fluid">

          <form [formGroup]="checkoutFormGroup" (ngSubmit)="onSubmit()">

              <!-- customer form group -->
              <div formGroupName="customer" class="form-area">
                  <h3>Customer</h3>

                  <div class="row">
                      <div class="col-md-2"> <label>First Name</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="firstName" type="text">
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Last Name</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="lastName" type="text">
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Email</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="email" type="text">
                          </div>
                      </div>
                  </div>

              </div>

              <!-- shipping Address -->
              <div formGroupName="shippingAddress" class="form-area">
                  <h3>Shipping Address</h3>

                  <div class="row">
                      <div class="col-md-2"> <label>Country</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <select formControlName="country">
                                  <option>TO DO</option>
                              </select>
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Street</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="street" type="text">
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>City</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="city" type="text">
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>State</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <select formControlName="state">
                                  <option>TO DO</option>
                              </select>
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Zip Code</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="zipCode" type="text">
                          </div>
                      </div>
                  </div>

              </div>

              <!-- check box 在相同的 Address -->

               <div class="input-space">
                 <label class="au-checkbox">
                   <input type="checkbox" (change)="copyShippingAddressToBillingAddress($event)">
                   <span class="au-checkbox"></span>Billing Address same as Shipping Address
                 </label>
               </div>





              <!-- Billing Address -->
              <div formGroupName="billingAddress" class="form-area">
                  <h3>Billing Address</h3>

                  <div class="row">
                      <div class="col-md-2"> <label>Country</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <select formControlName="country">
                                  <option>TO DO</option>
                              </select>
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Street</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="street" type="text">
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>City</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="city" type="text">
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>State</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <select formControlName="state">
                                  <option>TO DO</option>
                              </select>
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Zip Code</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="zipCode" type="text">
                          </div>
                      </div>
                  </div>

              </div>

              <!-- Credit Card -->
              <div formGroupName="creditCard" class="form-area">
                  <h3>Credit Card</h3>

                  <div class="row">
                      <div class="col-md-2"> <label>Card Type</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <select formControlName="cardType">
                                  <option>Visa</option>
                                  <option>Mastercard</option>
                              </select>
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Name on Card</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="nameOnCard" type="text">
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Card Number</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="cardNumber" type="text">
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Security Code</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <input formControlName="securityCode" type="text">
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Expiration Month</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <select formControlName="expirationMonth">
                                  <option>TO DO</option>
                              </select>
                          </div>
                      </div>
                  </div>

                  <div class="row">
                      <div class="col-md-2"> <label>Expiration Year</label></div>
                      <div class="col-md-9">
                          <div class="input-space">
                              <select formControlName="expirationYear">
                                  <option>TO DO</option>
                              </select>
                          </div>
                      </div>
                  </div>

              </div>

              <!-- Order details -->
              <div class="form-area">
                  <h3>Review Your Order</h3>

                  <p>Total Quantity: {{ totalQuantity }}</p>
                  <p>Shipping: FREE</p>
                  <p>Total Price: {{ totalPrice | currency: 'USD' }}</p>
              </div>

              <!-- submit button-->
              <div class="text-center">
                  <button type="submit" class="btn btn-info">Purchase</button>
              </div>

          </form>

      </div>
  </div>
</div>

要消除反紅的地方要到checkout.component.ts 檔案修正:
https://ithelp.ithome.com.tw/upload/images/20210907/20119035bCXP19pwnX.png

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-checkout',
  templateUrl: './checkout.component.html',
  styleUrls: ['./checkout.component.css']
})
export class CheckoutComponent implements OnInit {

  checkoutFormGroup: FormGroup;

  totalPrice: number = 0;
  totalQuantity: number = 0;

  constructor(private formBuilder: FormBuilder) { }

  ngOnInit(): void {

    this.checkoutFormGroup = this.formBuilder.group({

      customer:this.formBuilder.group({

          firstName:[''],
          lastName:[''],
          email:['']
        }),

        shippingAddress: this.formBuilder.group({
          street: [''],
          city: [''],
          state: [''],
          country: [''],
          zipCode: ['']



        }),
        billingAddress: this.formBuilder.group({
          street: [''],
          city: [''],
          state: [''],
          country: [''],
          zipCode: ['']



        }),
        creditCard: this.formBuilder.group({
          cardType: [''],
          nameOnCard: [''],
          cardNumber: [''],
          securityCode: [''],
          expirationMonth: [''],
          expirationYear: ['']



        })
    });
  }

  copyShippingAddressToBillingAddress(event){

    if(event.target.checked){
      this.checkoutFormGroup.controls.billingAddress
             .setValue(this.checkoutFormGroup.controls.shippingAddress.value);
    }
    else{
      this.checkoutFormGroup.controls.billingAddress.reset();
    }
    


  }

  onSubmit(){
    console.log("Handling the submit button");
    console.log(this.checkoutFormGroup.get('customer').value);
    console.log("The email address is "+this.checkoutFormGroup.get('customer').value.email);

  }

}

按V之後內容就會複寫到下面:
https://ithelp.ithome.com.tw/upload/images/20210907/20119035USoQbQyq65.png

這個得下一篇:https://ithelp.ithome.com.tw/articles/10258797


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言