ค้นหาเว็บไซต์

typescript - Angular ChangeDetectorRef: ไม่สามารถอ่านคุณสมบัติ 'detectChanges' ของ undefined


ฉันได้รับข้อผิดพลาดต่อไปนี้กับ ChangeDetectorRef ไม่แน่ใจว่าทำไมมันถึงเกิดขึ้นอย่างกะทันหัน ในขณะที่คอมโพเนนต์อื่นใช้ ChangeDetectorRef มีใครรู้วิธีแก้ไขไหม? มันเชื่อมโยงกับการเลือก Kendo Grid

TypeError: ไม่สามารถอ่านคุณสมบัติ 'detectChanges' ของ 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();
  }

เอชทีเอ็มแอล:

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

คำตอบ #1

น่าจะเป็นบริบทของ this (โดยบังเอิญ ฟังก์ชันนี้ถูกเรียกว่าอย่างไร?). แก้ไขได้โดยการเปลี่ยนเป็นฟังก์ชันลูกศร

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

บทความที่เกี่ยวข้อง