How to call a service from ag grid drag & drop event

我与影子孤独终老i 提交于 2021-01-29 19:12:52

问题


I have an ag-grid set up with options to trigger drag and drop events:

this.gridOptions = {
      columnDefs: this.getColumnDefs(),
      rowDragManaged: true,
      onRowDragEnter: this.onRowDragEnter,
      onRowDragEnd: this.onRowDragEnd,
...

I also have a simple service

export class myService {
...
   public doSomething(): void {
        ...
   }
}

It seems I cannot call the doSomething service function from within the onRowDragEnd event - getting a cannot read property 'doSomething' of undefined... at Object.onRowDragEnd error.

I know the service is defined and working correctly, doSomething can be called from the grid component's init function, so I'm just not sure how I can call something external to the grid or any workaround ?


回答1:


Arrow functions will execute in their lexical context regardless of the caller. Try this:

this.gridOptions = {
      columnDefs: this.getColumnDefs(),
      rowDragManaged: true,
      onRowDragEnter: (event: RowDragEvent) => {
           this.onRowDragEnter(event);
      },
      onRowDragEnd: (event: RowDragEvent) => {
           this.onRowDragEnd(event);
      }
...

Edit: I updated this answer so you can pass the params from the event to your handler.



来源:https://stackoverflow.com/questions/58433743/how-to-call-a-service-from-ag-grid-drag-drop-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!