我正在使用p-fullCalendar
控件来显示约会。
我想在标题中添加自定义按钮以添加新约会。
我通过在选项as per full calendar docs中指定它来完成此操作:
export class AppointmentsComponent implements OnInit {
events: any[];
options: any;
displayAddAppointment: boolean;
constructor() { }
ngOnInit() {
this.options = {
header: {
left: 'prev,next today',
center: 'title',
right: 'addAppointmentButton, month,agendaWeek,agendaDay'
},
customButtons: {
addAppointmentButton:{
text:"New appointment",
click: r=> {
//this works but displayAddAppointment within the component is inaccessible.
//I would like to display a modal dialog to add a new appointment from this.
}
}
}
};
html是:
<div>
<p-fullCalendar [events]="events" [options]="options"></p-fullCalendar>
<p-dialog header="Schedule new appointment"
[(visible)]="displayAddAppointment"
modal="modal">
</p-dialog>
<div>
按钮显示正常,并且单击事件也被触发。
但是如何处理此按钮的click事件,以便可以显示模式对话框?
点击事件中的this
是按钮本身:
答案 0 :(得分:1)
您可以设置_self = this
并使用此_self
变量
ngOnInit() {
let _self = this;
this.options = {
header: {
left: 'prev,next today',
center: 'title',
right: 'addAppointmentButton, month,agendaWeek,agendaDay'
},
customButtons: {
addAppointmentButton:{
text:"New appointment",
click: (r) => {
console.log(_self);
}
}
}
};
答案 1 :(得分:1)
将click事件绑定到组件方法并绑定(此):
addAppointmentButton: {
text: 'New appointment',
click: this.click.bind(this)
}
// ...
click() {
this.displayAddAppointment = !this.displayAddAppointment;
}
经过测试,可以正常工作。这样您就不会失去范围。