Install angular-calendar via ng add
mainThe recommended way to install the library is using the Angular CLI ng add command, which handles dependencies and configuration automatically.
ng add angular-calendarrepository·main·Indexed 25 days ago
https://github.com/mattlewis92/angular-calendarA calendar component for Angular 20.2+ that displays events in month, week, or day views. It supports date manipulation via date-fns or Moment.js adapters and provides customizable accessibility labels through CalendarA11y, custom date formatting via CalendarDateFormatterInterface, and event title customization using CalendarEventTitleFormatter.
The recommended way to install the library is using the Angular CLI ng add command, which handles dependencies and configuration automatically.
ng add angular-calendarIf you prefer manual installation, follow these three steps:
date-fns via npm.src/styles.css).DateAdapter using provideCalendar in your component's providers.```bash
npm install angular-calendar date-fns/* angular-cli file: src/styles.css */
@import "../node_modules/angular-calendar/css/angular-calendar.css";import { Component } from '@angular/core';
import { DateAdapter, provideCalendar, CalendarPreviousViewDirective, CalendarTodayDirective, CalendarNextViewDirective, CalendarMonthViewComponent, CalendarWeekViewComponent, CalendarDayViewComponent, CalendarEvent, CalendarView, CalendarDatePipe } from 'angular-calendar';
import { adapterFactory } from 'angular-calendar/date-adapters/date-fns';
@Component({
imports: [CalendarPreviousViewDirective, CalendarTodayDirective, CalendarNextViewDirective, CalendarMonthViewComponent, CalendarWeekViewComponent, CalendarDayViewComponent, CalendarDatePipe],
providers: [
provideCalendar({
provide: DateAdapter,
useFactory: adapterFactory,
}),
],
template: `
<button mwlCalendarPreviousView [view]=You can customize the accessibility (ARIA) labels used by the calendar by providing a custom implementation of the CalendarA11y class using Angular's Dependency Injection (DI). This allows you to change how dates, events, and landmarks are described to screen readers.
To implement this, extend the CalendarA11y class, override the specific methods you wish to change, and then provide your custom class in the providers array of the component that uses the calendar.
import { A11yParams, CalendarA11y } from 'angular-calendar';
import { formatDate } from '@angular/common';
import { Injectable } from '@angular/core';
// adding your own a11y params
export interface CustomA11yParams extends A11yParams {
isDrSuess?: boolean;
}
@Injectable()
export class CustomCalendarA11y extends CalendarA11y {
// overriding a function
public openDayEventsLandmark({ date, locale, isDrSuess }: CustomA11yParams): string {
if (isDrSuess) {
return `
${formatDate(date, 'EEEE MMMM d', locale)}
Today you are you! That is truer than true! There is no one alive
who is you-er than you!
`;
}
return super.openDayEventsLandmark({ date, locale });
}
}
// in your component that uses the calendar
@Component({
// ...
providers: [{
provide: CalendarA11y,
useClass: CustomCalendarA11y
}]
})
export class MyCalendarComponent { }When using ng add angular-calendar, you can specify several options to tailor the installation to your project structure:
dateAdapter: Choose between 'moment' or 'date-fns'. This determines which date library and adapter factory are configured.standalone: A boolean flag. If true, the schematic attempts to install the calendar into a standalone component. If false (default), it installs into an NgModule.projectName: The name of the project in your workspace to install into.installToPath: An optional path to a specific component or module where the calendar imports and providers should be added.You can customize how event titles and tooltips are displayed in different calendar views (month, week, day) by overriding the CalendarEventTitleFormatter class. To apply your custom logic, extend the CalendarEventTitleFormatter class and provide it in your component's providers array using the CalendarEventTitleFormatter token.
import { Injectable } from '@angular/core';
import { CalendarEventTitleFormatter, CalendarEvent } from 'angular-calendar';
@Injectable()
class CustomEventTitleFormatter extends CalendarEventTitleFormatter {
month(event: CalendarEvent): string {
return `Custom prefix: ${event.title}`;
}
}
// In your component
@Component({
// ...
providers: [{
provide: CalendarEventTitleFormatter,
useClass: CustomEventTitleFormatter
}]
})
class MyComponent {}By default, ng add angular-calendar configures your application using NgModule. The schematic will:
installToPath).imports array.provideCalendar provider to the module's providers array.If you run ng add angular-calendar --standalone, the schematic will:
installToPath or the default app component).angular-calendar to the component's imports array:CalendarPreviousViewDirectiveCalendarTodayDirectiveCalendarNextViewDirectiveCalendarMonthViewComponentCalendarWeekViewComponentCalendarDayViewComponentCalendarDatePipeDateAdapterprovideCalendarprovideCalendar provider to the component's providers array using the appropriate factory for your chosen dateAdapter.The mwlCalendarToday directive allows you to create a button or element that, when clicked, updates the calendar's view date to the current day. It uses two-way data binding with the viewDate property to ensure the calendar view synchronizes with the updated date.
To use it, apply the directive to an element (like a <button>) and bind it to your component's view date variable using [(viewDate)].
<button
mwlCalendarToday
[(viewDate)]="viewDate">
Today
</button>The mwl-calendar-day-view component displays all events for a single day. It is a wrapper around the week view configured for one day.
Basic usage requires providing a viewDate and an array of events.
To use Moment.js for all date formatting within the calendar, you must provide both the MOMENT injection token (with the moment library instance) and the CalendarMomentDateFormatter class as the CalendarDateFormatter. This allows the calendar to use Moment's locale and formatting capabilities for all view headers, titles, and time labels.
import { CalendarDateFormatter, CalendarMomentDateFormatter, MOMENT } from 'angular-calendar';
import moment from 'moment';
// in your component or module providers
provide: [{
provide: MOMENT, useValue: moment
}, {
provide: CalendarDateFormatter, useClass: CalendarMomentDateFormatter
}]The mwl-calendar-month-view component displays a monthly calendar view with events. You can pass in a viewDate to control which month is shown and an events array to populate the calendar.
<mwl-calendar-month-view
[viewDate="viewDate"
[events="events">
</mwl-calendar-month-view>If the default CalendarAngularDateFormatter (which uses Angular's formatDate pipe) does not meet your requirements, you can provide your own implementation of the CalendarDateFormatterInterface.
To customize the date strings displayed in the month, week, or day views, implement the following methods in your custom class:
monthViewColumnHeader({ date, locale }): Returns the week day labels for the month view header.monthViewDayNumber({ date, locale }): Returns the day number for month view cells.monthViewTitle({ date, locale }): Returns the title for the month view.weekViewColumnHeader({ date, locale }): Returns the week day labels for the week view header.weekViewColumnSubHeader({ date, locale }): Returns the sub-header day and month labels for the week view.weekViewTitle({ date, locale, weekStartsOn, excludeDays, daysInWeek }): Returns the title for the week view.weekViewHour({ date, locale }): Returns the time formatting for the left-hand side of the week view.dayViewHour({ date, locale }): Returns the time formatting for the left-hand side of the day view.dayViewTitle({ date, locale }): Returns the title for the day view.