How to use Angular DatePicker components as custom component?
This article explains how to use Syncfusion Angular DatePicker component as the custom component in Angular 5.
Pre-requisites
Make sure that you have the compatible versions of Angular in your machine before starting to work on this project.
- Node.js (latest version)
- Angular 5+
- Angular CLI
- TypeScript 2.6+
- Visual Studio code for editor
Introduction
Custom components are equal to components and they are the same thing. Before starting with this project, the Angular 5 DatePicker requires to add the Syncfusion ej2-angular-calendars package from npmjs, which are distributed in npm as @syncfusion scoped packages.
Creating Angular Project
To create the Angular project using the Angular CLI tool, follow the given steps.
Install Angular CLI 5 using following command.
npm install @angular/[email protected] -g
Now, create a new Angular project by using the command ng new and navigate to that folder.
ng new <project name> cd <project name>
Install the ej2-angular-calendars package through the npm install command.
npm install @syncfusion/ej2-angular-calendars --save
Run the application using the following command.
ng serve
To create custom component in the existing application
- Run the following command to create the custom component files in your application.
ng g c components/custom-component
- Then, import the DatePicker module in the “app.module.ts” file to register the DatePicker.
[app.module.ts]
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// import the DatePickerModule for the DatePicker component
import { DatePickerModule } from '@syncfusion/ej2-angular-calendars';
import { AppComponent } from './app.component';
@NgModule({
//declaration of DatePickerModule into NgModule
imports: [ BrowserModule, DatePickerModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
- Import the CSS files in your “custom-component.component.css” to load the DatePicker component styles.
[custom-component.component.css]
@import '../node_modules/@syncfusion/ej2-base/styles/material.css'; @import '../node_modules/@syncfusion/ej2-buttons/styles/material.css'; @import '../node_modules/@syncfusion/ej2-inputs/styles/material.css'; @import '../node_modules/@syncfusion/ej2-popups/styles/material.css'; @import '../node_modules/@syncfusion/ej2-lists/styles/material.css'; @import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css'; @import '../node_modules/@syncfusion/ej2-calendars/styles/material.css'; @import '../node_modules/@syncfusion/ej2-angular-calendars/styles/material.css';
- Add the DatePicker component in the custom component file as mentioned in the following code example.
[custom-component.component.html]
<ejs-datepicker [value]='dateValue' placeholder='Enter date'></ejs-datepicker>
[custom-component.component.ts]
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-custom-component',
templateUrl: './custom-component.component.html',
styleUrls: ['./custom-component.component.css'],
encapsulation: ViewEncapsulation.None
})
export class CustomComponentComponent implements OnInit {
constructor() { }
public dateValue = new Date();
ngOnInit() {
}
}
To render the code in the custom component page, import the custom component selector which you are created previously as mentioned in the following code example. Here, selector of custom component is “app-custom-component”.
[app.component.html]
<app-custom-component></app-custom-component>
Conclusion
I hope you enjoyed learning about how to use Angular DatePicker components as custom component.
You can
refer to our Angular DatePicker feature tour page to know about its other
groundbreaking feature representations and documentation, and how to quickly get started for
configuration specifications. You can also explore our
Angular DatePicker Example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!