ng2-handsontable从内部渲染器访问ng组件属性

时间:2017-09-18 15:16:19

标签: typescript handsontable

有没有办法从渲染器函数内部访问主机组件中定义的属性?我尝试使用关键字"这个"但它总是未定义的。

import * as Handsontable from 'handsontable';
import { Component } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: "my-component",
  templateUrl: "my-component.component.html"
})
export class MyComponentComponent
  {

  public  newColumns: any[] = [{
          data: 'A',
          renderer: this.styleRenderer
        }];

 public data :any[] = [{A:'1'}]
  constructor(
  ) {
  }

  public styleRenderer(instance, td, row, col, prop, value, cellProperties) {

    Handsontable.renderers.TextRenderer.apply(this, arguments);
    console.log('is this null ?:',this);  //<-  "this" is always undefined in the given context

  }


}

感谢。

2 个答案:

答案 0 :(得分:0)

尝试根据what I found here

const $component = this;

public newColumns: any[] = [{
  data: 'A',
  renderer: (instance, td, row, col, prop, value, cellProperties) => {    
    console.log('is this null ?:', $component);  //<-  Try using $component from above instead of this
  }
}];

答案 1 :(得分:0)

它通过使用箭头功能来工作:

    styleRenderer = (hotTable: any, td: HTMLElement, row: any, col: any,
        prop: any, value: string, cellProperties: any) => {
        // 'this' component can be referenced from here
    }