出现错误:Property' gridOptions'类型' HTMLInputElement'上不存在

时间:2016-10-03 22:44:03

标签: angular typescript

我有这些代码

table.component.ts:

export class TableComponent {
  private gridOptions:GridOptions;

  constructor() {
    this.gridOptions = <GridOptions>{};
    this.gridOptions.headerCellRenderer = this.headerCellRendererFunc;
  }

  private headerCellRendererFunc(e){
    console.log(e.value);
    if (e.value !== 'checkbox') {
      return e.value;
    }

    var cb = document.createElement('input');
    cb.setAttribute('type', 'checkbox');
    var eHeader = document.createElement('label');
    eHeader.appendChild(cb);
    cb.addEventListener('change', function(e) {
      if (e[0].checked) {
        this.gridOptions.api.selectAll();
      } else {
        this.gridOptions.api.deselectAll();
      }
    });

    return eHeader;
  }  
}

错误已开启:

cb.addEventListener('change', function(e) {
  if (e[0].checked) {
    this.gridOptions.api.selectAll();
  } else {
    this.gridOptions.api.deselectAll();
  }
});

它无法识别this.gridOptions。 似乎&#34;这个&#34; scope在addEventListener上。

如何引用TableComponent类的gridOptions?

1 个答案:

答案 0 :(得分:0)

使用箭头功能维护周围的this

cb.addEventListener('change', e => {
  if (e[0].checked) {
    this.gridOptions.api.selectAll();
  } else {
    this.gridOptions.api.deselectAll();
  }
});