Template-Driven Forms In Angular
Angular Template-driven Forms is one of two ways to create forms in the framework. In this article, We will learn how to create a simple Template-driven forms sample app. We begin by creating a simple HTML form with a few form elements. The ngForm directive will build the top-level FormGroup control and transform it to a Template-driven form. Then, for each of the HTML form elements, we use the ngModel directive to construct a FormControl instance. We will learn how to submit form data to the component class later on. We will also learn how to initialize or reset the form data, as well as how to access the data in the component class using data binding.
We strongly advise you to go through our Angular Forms article if you haven't already. We addressed the core concepts of the Angular Forms Module in that essay.
What is Template-driven form?
We provide behaviors/validations in Template Driven Forms by using directives and attributes in our template and letting it function behind the scenes. Because everything happens in Templates, the component class requires very minimal code. This differs from reactive forms, where the logic and controls are defined in the component class.
Forms that are driven by templates
- The ngForm directive is used to create the form,.
- The ngModel directive is used to create the controls.
- ngModel also has a two-way data binding feature.
- Validations are set by using directives in the template.
Forms that are based on templates are popular.
- The component class has a small amount of code.
- It's a lot easier to set up.
While they are doing so,
- It's difficult to dynamically add controls.
- Unit testing is a difficult task.
Create the Example Application
To create a new application, using following command.
ng new tdf --routing=true --style=css
Import FormsModule
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; //import FormsModule import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, FormsModule //Add in Imports Array ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
HTML Form
<form> <p> <label for="firstname">First Name</label> <input type="text" id="firstname" name="firstname"> </p> <p> <label for="lastname">Last Name</label> <input type="text" id="lastname" name="lastname"> </p> <p> <label for="email">Email </label> <input type="text" id="email" name="email"> </p> <p> <label for="gender">Geneder</label> <input type="radio" value="male" id="gender" name="gender"> Male <input type="radio" value="female" id="gender" name="gender"> Female </p> <p> <label for="isMarried">Married</label> <input type="checkbox" id="isMarried" name="isMarried"> </p> <p> <label for="country">country </label> <select name="country" id="country"> <option selected="" value=""></option> <option [ngValue]="c.id" *ngFor="let c of countryList"> {{c.name}} </option> </select> </p> <p> <button type="submit">Submit</button> </p> </form>
Component Class
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Template driven forms'; countryList:country[] = [ new country("1", "India"), new country('2', 'USA'), new country('3', 'England') ]; } export class country { id:string; name:string; constructor(id:string, name:string) { this.id=id; this.name=name; } }
ngForm
- Identifies itself with the <Form> directive.
- Creates a FormGroup instance at the top level.
- For each child control with the ngModel directive, creates a FormControl object.
- For each NgModelGroup directive, creates a FormGroup instance.
<form #contactForm="ngForm">
FormControl
<input type="text" name="firstname" ngModel>
Submit Form
<form #contactForm="ngForm" (ngSubmit)="onSubmit(contactForm)">
<form #contactForm="ngForm" (ngSubmit)="onSubmit(contactForm)"> <p> <label for="firstname">First Name</label> <input type="text" name="firstname" ngModel> </p> <p> <label for="lastname">Last Name</label> <input type="text" name="lastname" ngModel> </p> <p> <label for="email">Email </label> <input type="text" id="email" name="email" ngModel> </p> <p> <label for="gender">Geneder</label> <input type="radio" value="male" name="gender" ngModel> Male <input type="radio" value="female" name="gender" ngModel> Female </p> <p> <label for="isMarried">Married</label> <input type="checkbox" name="isMarried" ngModel> </p> <select name="country" ngModel> <option [ngValue]="c.id" *ngFor="let c of countryList"> {{c.name}} </option> </select> <p> <button type="submit">Submit</button> </p> </form>
Receive Form Data
onSubmit(contactForm) { console.log(contactForm.value); }
country: "1" firstname: "Sachin" email:"sachin@gmail.com" gender: "male" isMarried: true lastname: "Tendulkar"
Local Variable
ngForm
<form #contactForm="ngForm" (ngSubmit)="onSubmit(contactForm)">
We can now use some of the attributes and methods to determine the status of the form. As an example,
<p> <button type="submit">Submit</button> </p> <pre>Value : {{contactForm.value | json }} </pre> <pre>Valid : {{contactForm.valid}} </pre> <pre>Touched : {{contactForm.touched }} </pre> <pre>Submitted : {{contactForm.submitted }} </pre>
FormControl
<input type="text" name="firstname" #fname="ngModel" ngModel>
The variable #fname now contains a reference to the FormControl's firstname. The properties of FormControl, such as value, valid, isvalid, and tocuhed, can then be accessed.
<p> <label for="firstname">First Name </label> <input type="text" name="firstname" #fname="ngModel" ngModel> </p> <pre>Value : {{fname.value}} </pre> <pre>valid : {{fname.valid}} </pre> <pre>invalid : {{fname.invalid}} </pre> <pre>touched : {{fname.touched}} </pre>
Nested FormGroup
<form #contactForm="ngForm" (ngSubmit)="onSubmit(contactForm)">
<div ngModelGroup="address"> <p> <label for="city">City</label> <input type="text" name="city" ngModel> </p> <p> <label for="street">Street</label> <input type="text" name="street" ngModel> </p> <p> <label for="pincode">Pin Code</label> <input type="text" name="pincode" ngModel> </p> </div>
Now, run the application and fill out the form. As you can see, the end outcome is as follows.
Value : { "firstname": "Sachin", "lastname": "Tendulkar", "email":"sachin@gmail.com" "gender": "male", "isMarried": true, "country": "1", "address": { "city": "Mumbai", "street": "Fashin Street", "pincode": "400600" } }