使用TranslateService的未定义的gridOptions

时间:2018-04-26 14:41:34

标签: angular

我有一张gridOptions的表格,我可以很好地翻译数据,但如果我尝试翻译函数phaseSwitcher,我会遇到并发症。

constructor(private http: HttpClient, private translate: TranslateService) {
    this.gridOptions = <GridOptions>{};
     this.gridOptions = <GridOptions>{};

        this.translate.get(['projectList']).subscribe(res => {
          this.columnDefs = [
              headerName: res['projectList'][0]['client'],
              field: 'client',
              headerTooltip: res['projectList'][0]['client.tooltip'],
              width: 130
            }, 
            {
              headerName: res['projectList'][0]['fase'],
              field: 'phase',
              headerTooltip: res['projectList'][0]['fase.tooltip'],
              valueFormatter: this.phaseSwitcher,
              width: 110
            }
           ]}
          ];
        });
       }

我的client中的client.tooltipheaderName,...等我的值正确。

然后,我的问题方法来了:

 phaseSwitcher(params) {
     let phase;

    TranslateService.prototype.get(['text']).subscribe(val => {
      console.log('valoo ', val['text']);

      switch (params.value) {
        case 0:
          phase = 'Fase 0';
          break;
        case 1:
          phase = 'Fase 1';
          break;
        default:
          phase = 'Fase X';
          break;
      }
    });
    return phase;
  }

我收到此错误:

ERROR TypeError: Cannot read property 'translations' of undefined
    at Object.get [as translations] (core.es5.js:3076)
    at Object.webpackJsonp../node_modules/@ngx-translate/core/@ngx-translate/core.es5.js.TranslateService.get (core.es5.js:3314)
    at webpackJsonp../src/app/project-list/project-list.component.ts.ProjectListComponent.phaseSwitcher (project-list.component.ts:129)
    at ExpressionService.webpackJsonp../node_modules/ag-grid/dist/lib/valueService/expressionService.js.ExpressionService.evaluate (expressionService.js:35)
    at ValueFormatterService.webpackJsonp../node_modules/ag-grid/dist/lib/rendering/valueFormatterService.js.ValueFormatterService.formatValue (valueFormatterService.js:52)
    at CellComp.webpackJsonp../node_modules/ag-grid/dist/lib/rendering/cellComp.js.CellComp.getValueAndFormat (cellComp.js:669)
    at new CellComp (cellComp.js:55)
    at rowComp.js:927
    at Array.forEach (<anonymous>)
    at RowComp.webpackJsonp../node_modules/ag-grid/dist/lib/rendering/rowComp.js.RowComp.createCells (rowComp.js:926)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,为cellRenderer更改valueFormatter并创建了这个函数。遗憾。

 headerName: res['projectList'][0]['fase'],
          field: 'phase',
          headerTooltip: res['projectList'][0]['fase.tooltip'],
          cellRenderer: function(params) {
            let phase;
            switch (params.value) {
              case 0:
                phase = res['projectList'][0]['list.fase.0'];
                break;
              case 1:
                phase = res['projectList'][0]['list.fase.1'];
                break;
              default:
                phase = res['projectList'][0]['list.fase.x'];
                break;
            }
            return phase;
          },