首页 > 解决方案 > Angular ChangeDetectorRef:无法读取未定义的属性“detectChanges”

问题描述

我收到 ChangeDetectorRef 的以下错误。不知道为什么它突然发生,当其他组件使用 ChangeDetectorRef 时。有谁知道如何解决?它链接到剑道网格选择。

TypeError:无法读取未定义的属性“detectChanges”

export class DocumentPropertyGridComponent implements OnInit, OnChanges {

  public documentPropertyGridDataSelected: Array<DocumentPropertyGridData> = new Array<DocumentPropertyGridData>();

  constructor(private cdr: ChangeDetectorRef) { 
  }

  selectTest(e){
    this.documentPropertyGridDataSelected = e.selectedRows;
    this.cdr.detectChanges();
  }

HTML:

<div>
  Selected Count: {{documentPropertyGridDataSelected.length}}
<div>

标签: angulartypescriptangular8angular-changedetection

解决方案


可能是this上下文(顺便说一句,函数是如何调用的?)。可通过将其转换为箭头函数来修复

  selectTest = (e) => {
    this.documentPropertyGridDataSelected = e.selectedRows;
    this.cdr.detectChanges();
  }

推荐阅读