本文介绍了无法执行上下文菜单中的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用上下文菜单中的函数.

I'm trying to call a function in my context menu.

getContextMenuItems(params) {
    console.log(params.node.data)
    var result = [

      {
        name: "Delete",
        action : function () {
         this.deletePriceFactor(params.node.data);
        }
        ,
        cssClasses: ["redFont", "bold"]
      },
      {
        name: "Audit"
      }

    ]
      return result;
    }

 deletePriceFactor = (rowdata)  =>{
    this.priceFactorService.deleteEntry(rowdata.exchangeCode, rowdata.productCode, rowdata.secType).subscribe(pricefactors => {
    });

  }

我不断收到错误消息:错误类型错误:this.deletePriceFactor 不是函数在 Object.action (price-factor.component.ts:162)

I keep getting an error:ERROR TypeError: this.deletePriceFactor is not a function at Object.action (price-factor.component.ts:162)

我尝试过使用这样的箭头函数:

I have tried using arrow functions like this:

action : () =>  {
         this.deletePriceFactor(params.node.data);
        }

以上导致另一个错误:core.js:1673 ERROR TypeError: Cannot read property 'deletePriceFactor' of undefined

The above results in another error: core.js:1673 ERROR TypeError: Cannot read property 'deletePriceFactor' of undefined

推荐答案

如果你的 html 是这样的:

if your html is like:

<ag-grid-angular
      [getContextMenuItems]="getContextMenuItems"
      .
      .
      .
    ></ag-grid-angular>

那么函数 getContextMenuItems 必须写成:

then the function getContextMenuItems must be writen like :

getContextMenuItems = (params) => {
}

因此,this 关键字指向您的组件.

Hence, the this keyword points to your component.

之后,调用您的方法,如:

After that, call your method like:

action : () => this.deletePriceFactor(params.node.data)

这篇关于无法执行上下文菜单中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:39
查看更多