javascript - 如何使用 NgOnChanges 更新 FullCalendar 上的事件?
问题描述
我正在开发 Angular 7 上的预订房间应用程序。
我正在使用完整的日历来显示数据库中的事件。
FullCalendar 对一个房间运行良好,但每次我从另一个房间切换时,它都会初始化另一个日历。
我的代码看起来像这样。
ngOnChanges(changes: any) {
this.getAllReservations();
}
public getAllReservations() {
let apiUrl: string = `api/Reservations/Room=${this.selection}`;
this.repository.getData(apiUrl).subscribe(res => {
this.Reservations = res as Reservation[];
//full calendar intit
var calendarEl = document.getElementById('calendar');
var calendar = new Calendar(calendarEl, {
events: this.Reservations.map(function (x) {
return {
title: x.Note,
start: x.StartDate,
end: x.EndDate
};
}),
});
calendar.render();
},
(error) => {
this.errorHandler.handleError(error);
this.errorMessage = this.errorHandler.errorMessage;
})
}
我重复一遍,这段代码运行良好,我得到了我的事件。=> 证明。
但这每次我打电话给房间时都会在下面创建一个新日历,这是我的问题。
我完全明白了,但我找不到解决我的问题的解决方案。我试图在 ngOnInit 中隔离日历并只更新事件,但函数事件没有找到日历。
解决方案
好的 Christophe Sacchi,如果我假设您只使用您的 api 预订一个房间,这是正常的。您应该汇总每个房间的所有预订:
public getAllReservations() {
this.Reservations = [];
rooms.map(room => {
const apiUrl: string = `api/Reservations/Room=${room}`;
const roomReservations = this.repository.getData(apiUrl).subscribe(res => res as Reservation[],
(error) => {
this.errorHandler.handleError(error);
this.errorMessage = this.errorHandler.errorMessage;
});
this.Reservations.push(roomReservations);
});
//full calendar intit
const calendarEl = document.getElementById('calendar');
const calendar = new Calendar(calendarEl, {
events: this.Reservations.map(roomReservations => roomReservations.map(reservation => ({
title: reservation.Note,
start: reservation.StartDate,
end: reservation.EndDate
})),
),
});
calendar.render();
}
如果你尝试这样的事情(在没有测试的情况下内联完成,所以你可能需要稍微更新一下),它有效吗?
抱歉,我不是 Angular 开发人员。
推荐阅读
- angular - Angular7:如何通过检查内部循环中存在的子复选框来通过外部for循环中的ID检查父复选框?
- docker - 如何在存储在单独容器中的烧瓶 HTML 模板中引用?
- c - 在 c 中处理管道时关闭文件描述符
- javascript - 如何在javascript setInterval函数中显示毫秒和秒
- typescript - 带有打字稿的 RamdaJs,在无点时避免错误
- kubernetes - microk8s 连接到端口 16443 被拒绝
- azure - Azure - 获取用户刷新令牌失败
- python - ParserError("字符串不包含日期:%s", timestr) 错误
- javascript - Javascript 随机图像,随机过渡幻灯片
- python - matplotlib.pyplot.show() 的意义是什么?