Introduction

Forms are one of the most important parts of any business or enterprise application. Forms are used in many ways. They can be used during login, used in registering any new details, and so many other countless requirements. Forms are very important in guiding the end user towards effectively and efficiently going through the application workflow.

As a developer what we have to take care of includes:

For a better experience in applications having forms, we should take care of all the above points.

Prerequisites

  1. HTML, CSS, JavaScript
  2. Angular – Template, components, data binding, services, routing

Basic knowledge of Angular and understanding the above requirements is enough to start with Angular forms.

Forms In Angular

Template

The Component template contains the HTML view to collect the data from the end user.

Class

The component class handles the data binding and sends the data through to the service.

To achieve this Angular provides us with two approaches,

  1. Template-Driven forms
  2. Reactive/model driven Forms.

Template driven forms are used when most of the code is written in the component HTML template.

Reactive forms are used when most of the code is written in the component class.

For working with Angular forms we have to enable the type of form used. Let us start with Template-driven forms.

Enable template driven forms

Angular doesn’t give you functionality to use ngModel and other form-related tasks directly. We need to export them according to our usage,

  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import {FormsModule} from '@angular/forms';
  4. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
  5. @NgModule({
  6. declarations: [
  7. AppComponent,
  8. ],
  9. imports: [
  10. BrowserModule,
  11. FormsModule
  12. ],
  13. bootstrap: [AppComponent]
  14. })
  15. export class AppModule { }

We have enabled template driven forms by adding FormsModule to our application.

Some of the key points of the template-driven forms are:

Let us start with the simple application.

  1. Go to your project folder.
  2. Install latest angular cli.

    Run command : npm install -g @angular/@cli@latest
  1. Create new project > ng new TDFApplicatioin (template driven form application)
  2. > cd TDFApplication.

open app-component.ts and add the below contents,

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'app-root',
  4. templateUrl: './app.component.html',
  5. styleUrls: ['./app.component.css']
  6. })
  7. export class AppComponent {
  8. groups=['A+', 'A-', 'B+', 'B-', 'O+', 'O-'];
  9. }

Open app-component.html and add the below contents,

  1. <h3>
  2. Student Registration!
  3. </h3>
  4. <p>Enter the below fields for student details.</p>
  5. <div>
  6. <form>
  7. <div>
  8. <label>Enter name : </label>
  9. <input type="text">
  10. </div>
  11. <div>
  12. <label>Enter Email : </label>
  13. <input type="email">
  14. </div>
  15. <div>
  16. <label>Enter Phone no : </label>
  17. <input type="tel">
  18. </div>
  19. <div>
  20. <div>
  21. select blood group:
  22. </div>
  23. <div>
  24. <select>
  25. <option selected>A+</option>
  26. <option *ngFor="let group of groups">{{group}}</option>
  27. </select>
  28. </div>
  29. </div>
  30. <div>
  31. <div>Select course:</div>
  32. <div>
  33. <input type="radio" name="course" value="BCA">BCA
  34. </div>
  35. <div>
  36. <input type="radio" name="course" value="B.Tech">B.Tech
  37. </div>
  38. <div>
  39. <input type="radio" name="course" value="BE">BE
  40. </div>
  41. <div>
  42. <input type="radio" name="course" value="B.sc">B.sc
  43. </div>
  44. </div>
  45. <br>
  46. <div>
  47. <input type="checkbox"> <label>Send me updates.</label>
  48. </div>
  49. <div>
  50. <button type="submit">Submit</button>
  51. </div>
  52. </form>
  53. </div>

Now, we are done with creating forms and the next step is to capture the data input by the user to send it to the server.

Forms In Angular

In our next article, we will see how the data will be bind in template driven form approach.