Categories

Sep 18, 2019 in Angular

Q: angular forms

1 Answer

Sep 19, 2019
Forms in Angular

Forms in Angular take the standard capabilities of the HTML based forms and add an orchestration layer to help with creating custom form controls, and to supply great validation experiences. There are two parts to an Angular Reactive form, the objects that live in the component to store and manage the form, and the visualization of the form that lives in the template.

Define the checkout form model

First, you'll set up the checkout form model. The form model is the source of truth for the status of the form and is defined in the component class.

Open cart.component.ts.

Angular's FormBuilder service provides convenient methods for generating controls. As with the other services you've used, you need to import and inject the service before you can use it:

Import the FormBuilder service from the @angular/forms package.

src/app/cart/cart.component.ts

content_copy

import { Component } from '@angular/core';

import { FormBuilder } from '@angular/forms';

import { CartService } from '../cart.service';

The FormBuilder service is provided by the ReactiveFormsModule, which is already defined in the AppModule you modified previously (in app.module.ts).

Inject the FormBuilder service.

src/app/cart/cart.component.ts

content_copy

export class CartComponent {

  items;

  constructor(

    private cartService: CartService,

    private formBuilder: FormBuilder,

  ) {

  }

}

In the CartComponent class, define the checkoutForm property to store the form model.

src/app/cart/cart.component.ts

content_copy

export class CartComponent {

  items;

  checkoutForm;

}

During checkout, the app will prompt the user for a name and address. So that you can gather that information later, set the checkoutForm property with a form model containing name and address fields, using the FormBuilder#group() method.

src/app/cart/cart.component.ts

content_copy

export class CartComponent {

  items;

  checkoutForm;

  constructor(

    private cartService: CartService,

    private formBuilder: FormBuilder,

  ) {

    this.items = this.cartService.getItems();

    this.checkoutForm = this.formBuilder.group({

      name: '',

      address: ''

    });

  }

}

For the checkout process, users need to be able to submit the form data (their name and address). When the order is submitted, the form should reset and the cart should clear.

In cart.component.ts, define an onSubmit() method to process the form. Use the CartService#clearCart() method to empty the cart items and reset the form after it is submitted. (In a real-world app, this method also would submit the data to an external server.)

The entire cart component is shown below:

src/app/cart/cart.component.ts

content_copy

import { Component } from '@angular/core';

import { FormBuilder } from '@angular/forms';

import { CartService } from '../cart.service';

@Component({

  selector: 'app-cart',

  templateUrl: './cart.component.html',

  styleUrls: ['./cart.component.css']

})

export class CartComponent {

  items;

  checkoutForm;

  constructor(

    private cartService: CartService,

    private formBuilder: FormBuilder,

  ) {

    this.items = this.cartService.getItems();

    this.checkoutForm = this.formBuilder.group({

      name: '',

      address: ''

    });

  }

  onSubmit(customerData) {

    // Process checkout data here

    console.warn('Your order has been submitted', customerData);

    this.items = this.cartService.clearCart();

    this.checkoutForm.reset();

  }

}
Click here to read more about Angular JS
Click here to read more about Insurance

Related questions

Madanswer
Sep 21 in Angular
Feb 15 in Angular
Feb 15 in Angular
...