1

我有一个 Angular 2 组件,它实现了接口 IDatasource 作为 AG-GRID 的一部分

我无法从 dataSource getRows 函数中获取注入的 httpClient 服务。

我假设这与我不理解的注射有关;但我无法理解为什么。

constructor(private httpClient: HttpClient) {
    //This one works perfectly fine and httpClient is defined.
    console.log(this.httpClient);

    this.gridOptions = <GridOptions>{
        //Removed other config for brevity.
        datasource: {
            getRows: function (params) {
                let data: any[] = [];


                //this one - the httpClient is undefined. No idea why?
                console.log(this.httpClient);


                // this.httpClient.post('incidents/GetIncidentList', params).subscribe(response => data = response.json());
                let lastRow = -1;
                params.successCallback(data, lastRow);
            }
        }
    };
}

还尝试将其定义为单独的属性并得到完全相同的问题。

private dataSource: IDatasource = {
    getRows: function (params) {
        let data: any[] = [];
        console.log(this.httpClient);
        // this.httpClient.post('incidents/GetIncidentList', params).subscribe(response => data = response.json());
        let lastRow = -1;
        params.successCallback(data, lastRow);
    }
}
4

1 回答 1

2

如果你想使用 ´this inside the callback use() => instead offunction()`

getRows: function (params) {

应该

getRows: (params) => {
于 2017-01-26T15:08:28.850 回答