Angular 12 FullCalendar Dynamic Events Tutorial Example

Let’s learn about integrating fullcalendar in Angular 11/12 app in this tutorial. In addition, we will get to know about  displaying dynamic data or events in fullcalendar in this guide. In other words, we will get comprehensive knowledge on the above mentioned topic.

This Angular tutorial is compatible with version 4+ including latest version 12, 11, 10, 9, 8 ,7, 6 & 5.

How to Create FullCalendar and Display Dynamic Events in Angular 12

So, follow the below mentioned steps in order to create fullCalender and display dynamic events.

  • Step 1 is Creating the New Angular App
  • Step 2 is Installing the FullCalendar Library
  • Step 3 is Adding the Code on App.Module.ts File
  • Step 4 is Adding the Code on View File
  • Step 5 is Adding the Code On app.Component ts File
  • Step 6 is Creating the Dynamic Events in Angular
  • Step 7 is Starting the Angular App

Step 1 – Creating the New Angular App

First of all, we will see to open your terminal and execute the following command on it to install the Angular app:

ng new full-calender-app

Step 2 – Installing the FullCalendar Library

Further step is to install NPM package called @fullcalendar/angular. This will implement fullcalendar in angular 11 app. You can install the packages by executing the following commands on the terminal:

npm install @fullcalendar/angular 
 npm install @fullcalendar/daygrid
 npm install @fullcalendar/interaction

Step 3 – Adding Code on App.Module.ts File

The third step is also a simple one in which you visit src/app directory and open app.module.ts file. Afterwards, add the following lines into app.module.ts file:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
 
// import modules
import { HttpClientModule } from '@angular/common/http';
 
import { FullCalendarModule } from '@fullcalendar/angular'; 
import interactionPlugin from '@fullcalendar/interaction';
import dayGridPlugin from '@fullcalendar/daygrid';
 
FullCalendarModule.registerPlugins([ 
  interactionPlugin,
  dayGridPlugin
]);
 
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FullCalendarModule,
    HttpClientModule    
  ],
  providers: [],
  bootstrap: [AppComponent]
})
 
export class AppModule { }

Step 4 – Adding Code on View File

Fourth step is to create fullcalendar in Angular 11 app. Then, visit src/app/ and app.component.html and update the following code into it:

<div class="container">
  <full-calendar [options]="calendarOptions"></full-calendar>
</div>

Step 5 – Adding Code On app.Component ts File

In this step, you need to visit the src/app directory and open app.component.ts. Afterwards, add the following code into component.ts file:

import { Component } from '@angular/core';
 
import { HttpClient } from '@angular/common/http';
import { CalendarOptions } from '@fullcalendar/angular';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
 
export class AppComponent {
 
    Events = [];
    calendarOptions: CalendarOptions;
     
    constructor(private httpClient: HttpClient) { }
 
    onDateClick(res) {
      alert('Clicked on date : ' + res.dateStr)
    }
 
    ngOnInit(){
      setTimeout(() => {
        return this.httpClient.get('http://localhost:8888/event.php')
          .subscribe(res => {
              this.Events.push(res);
              console.log(this.Events);
          });
      }, 2200);
 
      setTimeout(() => {
        this.calendarOptions = {
          initialView: 'dayGridMonth',
          dateClick: this.onDateClick.bind(this),
          events: this.Events
        };
      }, 2500);
           
      }  
 
}

Step 6 – Creating Dynamic Events in Angular

In this step, you need to create event.php file and place the following code in the file:

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
 
$calendarEvents = array('title' => 'Event name', 'start' => '2021-05-10');
 
echo json_encode($calendarEvents);

Step 7 – Starting the Angular App

The final step is to execute the following command on terminal to start Angular fullcalendar npm app:

ng serve

 

Conclusion

So friends, in this tutorial we have tried to explain about integrating fullcalendar in Angular 11/12 app and displaying dynamic data or events in fullcalendar. Hope that the steps are quite simple and easy for understanding. Please do share with your friends to appreciate us.

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *