如何从Ag-Grid ValueFormatter调用服务

dev*_*per 2 typescript ag-grid angular

有谁知道如何从Angular2 +中的ag-grid的ValueFormatter调用服务?

例:

我在构造函数中注入了一个名为someService的服务,并希望从ValueFormatter中调用该服务。我应该怎么做才能做到这一点?

constructor(private service: SomeService) { }

this.columnDefs.push(
        {
            headerName: 'Date',
            width: 150,
            field: 'incidentDate',
            valueFormatter(params) {
                return this.service.doSomething(params.value);
            }
        });
Run Code Online (Sandbox Code Playgroud)

目前,它无法识别来自valueFormatter的服务。

提前致谢

dev*_*per 7

我已经找到解决问题的办法。我只需要执行以下操作:

 this.columnDefs.push(
    {
        headerName: 'Date',
        width: 150,
        field: 'incidentDate',
        valueFormatter: this.anotherFunction.bind(this)
    });

anotherFunction(params): string {
    return this.service.doSomething(params.value);
}
Run Code Online (Sandbox Code Playgroud)

  • 如您所见,“ this”不是您的组件。另一种选择是:`valueFormatter:(params)=> this.anotherFunction(params)`。 (2认同)