我正在使用AngularCalendar,尤其是可点击的日子功能。我需要以编程方式强制将dayClicked设置为默认值,例如当用户打开带有日历的页面时,他必须看到选择的今天。 我试图构造新的CalendarMonthViewDay,但是没有成功。 这是我在dayClickedEvent上的功能:
如何创建新的CalendarMonthViewDay以便Angular看到它并呈现选定日期的页面?
protected dayClicked(day: CalendarMonthViewDay): void {
// this.resetHoursInputs();
console.log(day);
if (day.isPast) {
return `enter code here`;
}
if (!this.multipleSelect) {
this.clearSelectedDays();
}
this.selectedMonthViewDay = day;
const selectedDateTime = this.selectedMonthViewDay.date.getTime();
const dateIndex = this.selectedDays.findIndex(
selectedDay => selectedDay.date.getTime() === selectedDateTime,
);
if (dateIndex > -1) {
delete this.selectedMonthViewDay.cssClass;
this.selectedDays.splice(dateIndex, 1);
}
this.selectedDays.push(this.selectedMonthViewDay);
day.cssClass = 'cal-day-selected';
this.selectedMonthViewDay = day;
// }
if (!this.multipleSelect) {
// this.updateDoctorAppointmentsList();
this.getSelectedDayAppointments();
// console.log();
}
console.log(day);
}
答案 0 :(得分:0)
您可以从示例中更改组件:
import { Component, AfterViewChecked, ViewChild, ChangeDetectorRef } from '@angular/core';
import { CalendarEvent } from 'angular-calendar';
@Component({
selector: 'mwl-demo-component',
templateUrl: 'template.html'
})
export class DemoComponent implements AfterViewChecked {
view: string = 'month';
viewDate: Date = new Date();
events: CalendarEvent[] = [];
clickedDate: Date;
@ViewChild('monthView') monthView;
constructor(private cdr: ChangeDetectorRef) {}
ngAfterViewChecked() {
const emittedValue = {
day: {
badgeTotal: 0,
date: new Date(),
events: [],
inMonth: true,
isFuture: false,
isPast: false,
isToday: true,
isWeekend: false,
},
};
this.monthView.dayClicked.emit(emittedValue);
this.cdr.detectChanges();
}
}
不要忘记将变量monthView
添加到mwl-calendar-month-view
:
<mwl-calendar-month-view
#monthView
*ngSwitchCase="'month'"
[viewDate]="viewDate"
[events]="events"
(dayClicked)="clickedDate = $event.day.date">
</mwl-calendar-month-view>
答案 1 :(得分:0)
好吧,我在html中使用了以下代码:
(dayClicked)="dayClicked($event.day)"
它似乎几乎可以正常工作,因为选择了我的日期,但是无论如何,即使它在css中具有“ cal-day-selected”类,其背景颜色也没有改变:
我的CSS:
.cal-day-selected,.cal-day-selected:hover
{
background-color: rgba($light-green-color-rgb, 0.7) !important;
}