You need to enable JavaScript to run this app. Angular 10项目中FullCalendar无法展示服务拉取的日程事件
优惠活动
大模型
产品
解决方案
定价
更多

Angular 10项目中FullCalendar无法展示服务拉取的日程事件

FullCalendar 集成问题
  • 项目已集成FullCalendar日程组件,当前存在的核心问题为:数据库存储的已有日程事件无法正常渲染展示到日历视图上
  • 已完成的验证:通过自定义Angular服务接口拉取事件数据,已确认拉取到的事件信息可正常打印到浏览器控制台,但数据始终无法加载到日历视图中
  • 当前开发环境为Angular CLI 10.1.7,参考GitHub示例片段完成了日历的基础实现,暂未定位到代码遗漏的根因

拉取到事件数据的控制台截图如下:
控制台返回事件数据示例

相关实现代码

component.ts 组件代码

import { Component, 
         OnInit, 
         ViewChild, 
         TemplateRef, 
         Input, 
         forwardRef,
         AfterViewChecked,
         ElementRef 
         } from '@angular/core';
//import { MatDialog } from "@angular/material/dialog";
import { FullCalendarComponent, 
         CalendarOptions, 
         DateSelectArg, 
         EventClickArg, 
         EventApi,
         EventInput, 
         Calendar,
         formatDate
        } from '@fullcalendar/angular';
import { FormControl, FormGroup } from "@angular/forms";
import dayGridPlugin from '@fullcalendar/daygrid';
import timeGrigPlugin from "@fullcalendar/timegrid";
import interactionPlugin from '@fullcalendar/interaction';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { TimeTableService } from '../../services/timetable.service';
import { Timetable } from '../../models/timetable';
import { global } from '../../services/global';

declare var $: any;


@Component({
  selector: 'app-my-calendar',
  templateUrl: './my-calendar.component.html',
  styleUrls: ['./my-calendar.component.css'],
  providers: [TimeTableService]
})

export class MyCalendarComponent implements OnInit {
    public page_title:string;
    public identity;
    public token;
    public timetable: Timetable;
    public status:string;
    public dates;
    public date;
    public url;

    public calendarOptions: CalendarOptions;
    public eventsModel: any;

    @ViewChild('fullcalendar' , { static: true }) calendarComponent: FullCalendarComponent;
    @ViewChild('createEvent', { static: true }) createEvent: TemplateRef<any>;

    dateForm: FormGroup;
    eventsCalendar: any[] = [];
    events: any[] = [];
    calendarEvents: EventInput[] = [];
    calendarPlugins = [dayGridPlugin, timeGrigPlugin, interactionPlugin];
    calendarApi: Calendar;
    initialized = false;


    constructor(
        private modal: NgbModal,
        private _route: ActivatedRoute,
        private _router: Router,
        private _timeTableService: TimeTableService,
    ){
        this.page_title = "Aqui puedes controlar tus citas";
        this.identity = this._therapistService.getIdentity();
        this.timetable = new Timetable(1,'','','','','');
        this.url = global.url;
        this.calendarEvents = [];
    }

    ngOnInit(): void {
        forwardRef(() => Calendar); 
        this.getTimeTable();
        this.loadEvents();
        

        this.calendarOptions = {
            timeZone: 'local',
            locale:   'es',
            plugins: [dayGridPlugin, interactionPlugin],
            headerToolbar: {
                left:   'prev,next today myCustomButton',
                center: 'title',
                right:  'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
            },
            buttonText: {
                today:  'Hoy',
                month:  'Mes',
                week:   'Semana',
                day:    'Día',
                list:   'Lista'
            },
            customButtons: {
                myCustomButton: {
                    text: 'Crear!',
                    //click: this.handleDateClick.bind(this),
                    click: function() { 
                        $('#crearcita').modal('show');
                    }
                }
            },
            initialView: 'dayGridMonth',
            allDaySlot: false,
            defaultTimedEventDuration: '00:45:00',
            forceEventDuration: true,
            weekends: true,
            editable: true,
            selectable: true,
            selectMirror: true,
            dayMaxEvents: true,

            dateClick: this.handleDateClick.bind(this), // bind is important! 
            eventClick: this.handleEventClick.bind(this),
            eventDragStop: this.handleEventDragStop.bind(this),
        };
    }

    getTimeTable(){
        this._timeTableService.getTimeTable().subscribe(
            response => {
                if(response.status == 'success'){
                    this.dates = response.dates;
                    console.log(this.dates);
                    this.dates.forEach(e => {
                        let calendarevent = {
                            //startEditable: false,
                            id: e.id,
                            title: e.title,
                            start: new Date(e.date).toISOString(),
                            //allDay: true,
                        };
                        console.log(calendarevent);             
                        this.eventsCalendar.push(calendarevent);
                    });
                    this.loadEvents();
                }
            },
            error => {
                console.log(error);
            }
        );
    }

    loadEvents() {
        this.calendarEvents =  this.eventsCalendar;
        console.log(this.eventsCalendar);
    }
}

HTML模板代码

<full-calendar
  *ngIf="calendarOptions"
  #fullcalendar
  [options]="calendarOptions"
  [events]="eventsCalendar"
  (eventClick)="onEventClick($event)"
></full-calendar>

内容的提问来源于stack exchange,提问作者GlebBrovar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.02 00:54:01