Template driven forms in Angular Daterangepicker component

27 Apr 20244 minutes to read

The form can be build with Angular template syntax easily along with form specific directives.
This template-driven forms uses the ng directives in view to handle the forms controls.

  • To enable the template-driven, import the FormsModule into corresponding app component.

For more details about template-driven Forms refer to:https://angular.io/guide/forms#template-driven-forms.

  • In angular forms mentioning the name is must to process as form elements.

  • Mention the name attribute to DateRangePicker element which will be used to identify the form element. To register an DateRangePicker element to ngForm, give the ngModel to it so the FormsModule will automatically detect the DateRangePicker as a form element.
    After that, the DateRangePicker value will be selected based on the ngModel value.

The following example demonstrates template driven forms with DateRangePicker component.

import { BrowserModule } from '@angular/platform-browser'
import { NgModule } from '@angular/core'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { DateRangePickerModule } from '@syncfusion/ej2-angular-calendars'

import { Component,ViewChild, ViewEncapsulation, Inject } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { DateRangePickerComponent } from '@syncfusion/ej2-angular-calendars';

class User {
    public range?: Date[];
    constructor() {

imports: [ FormsModule, ReactiveFormsModule,  DateRangePickerModule],

standalone: true,
    selector: 'app-root',
    templateUrl: './template.html'
export class DefaultDateRangePickerComponent {

    user?: User | any;
    ngOnInit() {
        this.user = new User();

    onSubmit(userForm: any) {
        (userForm.valid) ? alert("submitted"): alert("form is invalid");
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
  <form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)">
    <div class="form-group">
      <ejs-daterangepicker id="daterangepicker" name="date"  [(ngModel)]="user.date" #date="ngModel" width="350px" required></ejs-daterangepicker>
        <div  *ngIf="userForm.invalid && (userForm.dirty || userForm.touched)" class="alert alert-danger formerror">
          Valid date is required
        <div *ngIf="userForm.valid && (userForm.dirty || userForm.touched)" class="alert alert-success formerror">
              <td style="width:50%">Selected value: </td>
              <td class="formtext "> -  </td>
    <div class="buttons">
    <button type="submit" class="e-btn e-success">Submit</button>
    <button type="reset" class="e-btn e-warning">Reset</button>

  <div class="dataclass">userForm.value: </div>
  <div class="dataclass">userForm.valid: </div>


  form {
    margin-top: 50px;