ag-grid:对特定列使用网格事件 CellClicked - 无法访问“此”

gar*_*may 2 typescript ag-grid angular

我在网格中有一个列,单击该列时我试图访问类的某些方法,但this上下文已更改且不可用。前任 -

export class PreTflGridComponent implements OnInit {  

  columnDefs = [
    {onCellClicked: this.editCellClicked}
  ];

  constructor() { }   

  method(): void {
      console.log('working');
  }

  editCellClicked(params) {
    this.method();
  }  

}
Run Code Online (Sandbox Code Playgroud)

错误:

ERROR TypeError: this.method is not a function
Run Code Online (Sandbox Code Playgroud)

this上下文更改后如何访问方法/属性。

请参阅下面的确切问题 https://stackblitz.com/edit/angular-vwgcc2,如果您单击 make 列并打开控制台,那么它将显示我正在伪造的错误。

Viq*_*qas 5

this似乎给了你实际的行。您需要做的是通过绑定来告诉您的makeCellClicked方法实际上是什么:thisthis

onCellClicked: this.makeCellClicked.bind(this)

另请参阅更新的StackBlitz。