【问题标题】:How to handle custom button click event for PrimeNG p-fullCalendar?如何处理 PrimeNG p-fullCalendar 的自定义按钮单击事件?
【发布时间】:2019-01-18 15:44:57
【问题描述】:

我正在使用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>

按钮显示正常,点击事件也被触发。

但是如何处理这个按钮的点击事件,以便显示模态对话框呢?

点击事件中的this 是按钮本身:

【问题讨论】:

  • 点击事件中this指的是什么?
  • @MarkusDresch 我已更新问题以回答您的问题

标签: angular primeng primeng-calendar


【解决方案1】:

将点击事件绑定到组件方法并绑定(this):

addAppointmentButton: {
    text: 'New appointment',
    click: this.click.bind(this)
}
// ...
click() {
    this.displayAddAppointment = !this.displayAddAppointment;
}

经过测试并且有效。这样您就不会失去范围。

【讨论】:

    【解决方案2】:

    您可以设置_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);
              }
            }
          }
        };
    

    【讨论】:

    • 你试过了吗?将它分配给变量和访问变量与直接访问它之间没有区别。这是一回事。它不起作用
    猜你喜欢
    • 1970-01-01
    • 2017-09-03
    • 2011-10-16
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多