Template driven forms in Angular Datetimepicker component

17 Nov 20222 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 DateTimePicker element which will be used to identify the form element. To register an DateTimePicker element to ngForm, give the ngModel to it so the FormsModule will automatically detect the DateTimePicker as a form element.
    After that, the DateTimePicker value will be selected based on the ngModel value.

The following example demonstrates template driven forms with DateTimePicker component.

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

class User {
    constructor() {
        public datetime: Date
    }
}

@Component({
    selector: 'app-root',
    templateUrl: './app/template.html'
})
export class DefaultDateTimePickerComponent {
    constructor() {}
    user: User;
    ngOnInit() {
        this.user = new User(null);
    }

    onSubmit(userForm) {
        (userForm.valid) ? alert("submitted"): alert("form is invalid");
    }

}
import { DefaultDateTimePickerComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { DateTimePickerModule } from '@syncfusion/ej2-angular-calendars';


@NgModule({
    imports: [ FormsModule, ReactiveFormsModule, BrowserModule, DateTimePickerModule],
declarations: [DefaultDateTimePickerComponent],
bootstrap: [DefaultDateTimePickerComponent]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);