Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fullcalendar and Angular 14 – accessing component functions

This is probably something simple, anyway for some reason I cannot access my component functions from fullcalendar configuration object.

I followed the official doc on how to setup fullcalendar in Angular 14 : https://fullcalendar.io/docs/v6/angular

import { Component, OnInit } from '@angular/core';
import { CalendarOptions, defineFullCalendarElement } from '@fullcalendar/web-component';
import dayGridPlugin from '@fullcalendar/daygrid';
import { MyHttpService } from '...';

defineFullCalendarElement();

@Component({
    selector: 'app-calendar',
    templateUrl: './calendar.component.html',
    styleUrls: ['./calendar.component.css']
})
export class CalendarComponent implements OnInit {

test:number = 1;
calendarOptions: CalendarOptions = {
    plugins: [dayGridPlugin],
    headerToolbar: {
        left: 'prev,next today',
        center: 'title',
        right: 'dayGridMonth,dayGridWeek,dayGridDay'
    },
    events: function (info, successCallback, failureCallback) {
            console.log(this.test) //Cannot read properties of null (reading 'test')
            this.myHttpService.getEvents() //Cannot read properties of null (reading 'myHttpService')
                .subscribe((events) => {
                    successCallback(this.makeEvents(events));
                });
            },

    };

/**/
constructor(protected myHttpService : MyHttpService) { }

Basically I cannot access "this." inside the events function. Is this normal ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

currently this is referring to the calendarOptions, instead of creating a events function using function keyword, create it using arrow function.

example:

events: (info, successCallback, failureCallback)=>{}

you should be able to access this.test

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading