无法在jquery中调用角度函数

发布于 2025-01-11 09:52:37 字数 4059 浏览 0 评论 0原文

我正在使用角度完整日历插件,并在事件渲染功能中添加了上下文菜单。 上下文菜单正在工作,但我想在单击上下文菜单项时调用角度函数。

我使用了以下代码,但无法在 jquery 内调用“cancelRsvClick”。 输入图片这里的描述

       cancelRsvClick() {
        console.log('cancel---')
      }
      
      eventRender(event, element) {
        
          const contextmenu =  `<div  class="contextMenu ngx-contextmenu dropdown clearfix">
          <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu"
          style="display:block;position:static;margin-bottom:5px;">
              <li data-action = "CancelReservation"  ><a tabindex="-1" role="button"   >Cancel Reservation</a></li>
              <li data-action = "Billing" > <a tabindex="-1"  >Billing</a></li>
          </ul>
      </div>`;
      
        $(element).contextmenu((e) => {
          
          $('.contextMenu').remove();
          const $contextMenu = $(contextmenu).appendTo('body');
          $($contextMenu).css('top', e.pageY);
          $($contextMenu).css('left', e.pageX);
          $($contextMenu).css('position', 'absolute');
          $($contextMenu).css('overflow', 'hidden');
          $($contextMenu).css('z-index', 10000);
      
         
            $($contextMenu).on('click', 'li', (ele) => {
              console.log(ele);
              this.cancelRsvClick();
            });
         
          
        }); 

}

html代码

<app-clovatel-rm-sheduler
[header]="header"           
[footer]="footer"
[views]="views"
[defaultView]="defaultView"
[defaultDate]="defaultDate"
[contentHeight]="contentHeight"
[slotWidth]="slotWidth"
[slotDuration]="slotDuration"
[slotLabelFormat]="slotLabelFormat" 
[resourceAreaWidth]="resourceAreaWidth"
[resourceColumns]="resourceColumns"
[resources]="resources"
[restrictedResources]="restrictedResources"
[resourceOrder]="resourceOrder"
[allDayDefault]="allDayDefault"
[events]="events"
[eventOverlap]="eventOverlap"
[eventConstraint]="eventConstraint"
[selectConstraint]="selectConstraint"
(onSelect)="onDaySelect($event, this)"
(onEventClick)="onEventClick($event)"
(onDayRightClick)="onDayRightClick($event)"
(onEventMouseover)="onEventMouseover($event)"
[eventRender]="eventRender"
></app-clovatel-rm-sheduler>

这是我实现fullcallendar的方式。

@Input() resources: any[];
  
  @Input() events: any[];
  
  @Input() eventRender: Function;

  @Input() dayRender: Function;

  @Input() options: any;

schedule: any;

 constructor(public el: ElementRef, differs: IterableDiffers) {
    this.differ = differs.find([]).create(null);
    this.initialized = false;
  }

ngOnInit() {
    this.config = {
      resources: this.resources,
      resourceColumns: this.resourceColumns,
      resourceAreaWidth: this.resourceAreaWidth,
      resourceLabelText: this.resourceLabelText,    
      views: this.views,
      theme: true,
      header: this.header,
      footer: this.footer,
      selectable: this.selectable,
      droppable: this.droppable,     
      eventRender: this.eventRender,
      dayRender: this.dayRender,
      }
    }

 ngAfterViewChecked() {
    
    if (!this.initialized && this.el.nativeElement.offsetParent) {
      this.initialize();
    }
  }
  
 
  
  initialize() {
    this.schedule = jQuery(this.el.nativeElement.children[0]);
    this.schedule.fullCalendar(this.config);
    this.schedule.fullCalendar('addEventSource', this.events);
    this.initialized = true;
  }
  
    ngDoCheck() {
  
        const changes = this.differ.diff(this.events);

        if (this.schedule && changes) {
          this.schedule.fullCalendar('removeEventSources');
          this.schedule.fullCalendar('addEventSource', this.events);
        }
    }

  ngOnDestroy() {
    
    jQuery(this.el.nativeElement.children[0]).fullCalendar('destroy');
    this.initialized = false;
    this.schedule = null;
  }

i am using angular full calendar plugin and added a context menu in event render function.
context menu is working, but i want to call a angular function when context menu item was clickedy.

i have used following code but "cancelRsvClick" cannot be call inside jquery.
enter image description here

       cancelRsvClick() {
        console.log('cancel---')
      }
      
      eventRender(event, element) {
        
          const contextmenu =  `<div  class="contextMenu ngx-contextmenu dropdown clearfix">
          <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu"
          style="display:block;position:static;margin-bottom:5px;">
              <li data-action = "CancelReservation"  ><a tabindex="-1" role="button"   >Cancel Reservation</a></li>
              <li data-action = "Billing" > <a tabindex="-1"  >Billing</a></li>
          </ul>
      </div>`;
      
        $(element).contextmenu((e) => {
          
          $('.contextMenu').remove();
          const $contextMenu = $(contextmenu).appendTo('body');
          $($contextMenu).css('top', e.pageY);
          $($contextMenu).css('left', e.pageX);
          $($contextMenu).css('position', 'absolute');
          $($contextMenu).css('overflow', 'hidden');
          $($contextMenu).css('z-index', 10000);
      
         
            $($contextMenu).on('click', 'li', (ele) => {
              console.log(ele);
              this.cancelRsvClick();
            });
         
          
        }); 

}

html code

<app-clovatel-rm-sheduler
[header]="header"           
[footer]="footer"
[views]="views"
[defaultView]="defaultView"
[defaultDate]="defaultDate"
[contentHeight]="contentHeight"
[slotWidth]="slotWidth"
[slotDuration]="slotDuration"
[slotLabelFormat]="slotLabelFormat" 
[resourceAreaWidth]="resourceAreaWidth"
[resourceColumns]="resourceColumns"
[resources]="resources"
[restrictedResources]="restrictedResources"
[resourceOrder]="resourceOrder"
[allDayDefault]="allDayDefault"
[events]="events"
[eventOverlap]="eventOverlap"
[eventConstraint]="eventConstraint"
[selectConstraint]="selectConstraint"
(onSelect)="onDaySelect($event, this)"
(onEventClick)="onEventClick($event)"
(onDayRightClick)="onDayRightClick($event)"
(onEventMouseover)="onEventMouseover($event)"
[eventRender]="eventRender"
></app-clovatel-rm-sheduler>

this is how i implement fullcallendar.

@Input() resources: any[];
  
  @Input() events: any[];
  
  @Input() eventRender: Function;

  @Input() dayRender: Function;

  @Input() options: any;

schedule: any;

 constructor(public el: ElementRef, differs: IterableDiffers) {
    this.differ = differs.find([]).create(null);
    this.initialized = false;
  }

ngOnInit() {
    this.config = {
      resources: this.resources,
      resourceColumns: this.resourceColumns,
      resourceAreaWidth: this.resourceAreaWidth,
      resourceLabelText: this.resourceLabelText,    
      views: this.views,
      theme: true,
      header: this.header,
      footer: this.footer,
      selectable: this.selectable,
      droppable: this.droppable,     
      eventRender: this.eventRender,
      dayRender: this.dayRender,
      }
    }

 ngAfterViewChecked() {
    
    if (!this.initialized && this.el.nativeElement.offsetParent) {
      this.initialize();
    }
  }
  
 
  
  initialize() {
    this.schedule = jQuery(this.el.nativeElement.children[0]);
    this.schedule.fullCalendar(this.config);
    this.schedule.fullCalendar('addEventSource', this.events);
    this.initialized = true;
  }
  
    ngDoCheck() {
  
        const changes = this.differ.diff(this.events);

        if (this.schedule && changes) {
          this.schedule.fullCalendar('removeEventSources');
          this.schedule.fullCalendar('addEventSource', this.events);
        }
    }

  ngOnDestroy() {
    
    jQuery(this.el.nativeElement.children[0]).fullCalendar('destroy');
    this.initialized = false;
    this.schedule = null;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2025-01-18 09:52:37

如果您想在 jquery onClick 事件处理程序中使用角度类引用,请尝试使用 bind

$($contextMenu).on('click', 'li', (ele) => {
    console.log(ele);
    this.cancelRsvClick();
}).bind(this);

// or 

const $this = this;
$($contextMenu).on('click', 'li', (ele) => {
    console.log(ele);
    $this.cancelRsvClick();
}).bind(this);

// or 

eventRender(event, element) {
    const $this = this;
    const contextmenu =  `
        <div  class="contextMenu ngx-contextmenu dropdown clearfix">
            <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu"
            style="display:block;position:static;margin-bottom:5px;">
                <li data-action = "CancelReservation"  ><a tabindex="-1" role="button"   >Cancel Reservation</a></li>
                <li data-action = "Billing" > <a tabindex="-1"  >Billing</a></li>
            </ul>
        </div>
    `;
$(element).contextmenu((e) => {
    $('.contextMenu').remove();
    const $contextMenu = $(contextmenu).appendTo('body');
    $($contextMenu).css('top', e.pageY);
    $($contextMenu).css('left', e.pageX);
    $($contextMenu).css('position', 'absolute');
    $($contextMenu).css('overflow', 'hidden');
    $($contextMenu).css('z-index', 10000);
    $($contextMenu).on('click', 'li', (ele) => {
        console.log(ele);
        // Chage this to $this 
        $this.cancelRsvClick();
    });
}); 

If you want to use angular class reference inside jquery onClick event handler try using bind

$($contextMenu).on('click', 'li', (ele) => {
    console.log(ele);
    this.cancelRsvClick();
}).bind(this);

// or 

const $this = this;
$($contextMenu).on('click', 'li', (ele) => {
    console.log(ele);
    $this.cancelRsvClick();
}).bind(this);

// or 

eventRender(event, element) {
    const $this = this;
    const contextmenu =  `
        <div  class="contextMenu ngx-contextmenu dropdown clearfix">
            <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu"
            style="display:block;position:static;margin-bottom:5px;">
                <li data-action = "CancelReservation"  ><a tabindex="-1" role="button"   >Cancel Reservation</a></li>
                <li data-action = "Billing" > <a tabindex="-1"  >Billing</a></li>
            </ul>
        </div>
    `;
$(element).contextmenu((e) => {
    $('.contextMenu').remove();
    const $contextMenu = $(contextmenu).appendTo('body');
    $($contextMenu).css('top', e.pageY);
    $($contextMenu).css('left', e.pageX);
    $($contextMenu).css('position', 'absolute');
    $($contextMenu).css('overflow', 'hidden');
    $($contextMenu).css('z-index', 10000);
    $($contextMenu).on('click', 'li', (ele) => {
        console.log(ele);
        // Chage this to $this 
        $this.cancelRsvClick();
    });
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文