3

I am receiving the following error with ChangeDetectorRef. Not sure why its suddenly occurring, when other components utilize ChangeDetectorRef. Does anyone know how to solve? Its linking to a Kendo Grid selection.

TypeError: Cannot read property 'detectChanges' of undefined

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>

1 Answers1

7

Probably the this context (BTW, how is the function called?). Fixable by converting it to an arrow function

  selectTest = (e) => {
    this.documentPropertyGridDataSelected = e.selectedRows;
    this.cdr.detectChanges();
  }
mbojko
  • 13,503
  • 1
  • 16
  • 26
  • Wow! I was passing a private function as a callback reference to a subscribe! It seems JavaScript was immediately executing the function content instead of calling it later on after subscribe call is triggered. Thank you so much! – Samer Mar 18 '21 at 17:19